在 NestJS 中直接使用 HTTP2

Kon*_*cht 5 javascript nestjs

有没有办法直接在 NestJs 中使用 http2?(诸如pushStream等方法)还是我应该使用 express 来实现这种功能?

fed*_*der 0

没有。到目前为止还没有。但只要你有最新的nodejs,你就可以使用http2库。

您有 2 个选择:

  1. 编写一个Nestjs 自定义传输(我想知道到目前为止还没有人这样做过)
  2. 自己将其混合到您的代码中,例如这里使用 JS http2 客户端

const http2 = require('http2')

const session = http2.connect('http://localhost:8088')
    
session.on('error', (err) => console.error(err))

const body = {
                sql: "SELECT * FROM SIGNALS EMIT CHANGES;",
                properties: {"ksql.streams.auto.offset.reset": "latest"}
            }


const req = session.request({
    ':method': 'POST',
    ':path': '/query-stream',
    'Content-Type': 'application/json'
    })

req.write(JSON.stringify(body), 'utf8')
req.end()

req.on('response', (headers) => {
  // we can log each response header here
  for (const name in headers) {
    console.log("a header: " + '${name}: ${headers[name]}')
  }
})


req.setEncoding('utf8')
let data = ''
    
req.on('data', (chunk) => { 
    data += chunk 
    console.log('\n${data}')
})
    
req.on('end', () => {
  console.log('\n${data}')      
  session.close()
})
Run Code Online (Sandbox Code Playgroud)