有没有办法直接在 NestJs 中使用 http2?(诸如pushStream等方法)还是我应该使用 express 来实现这种功能?
没有。到目前为止还没有。但只要你有最新的nodejs,你就可以使用http2库。
您有 2 个选择:
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)