简体   繁体   English

在 NestJS 中直接使用 HTTP2

[英]Using HTTP2 directly in NestJS

Is there any way to use http2 directly in NestJs?有没有办法直接在 NestJs 中使用 http2? (methods like pushStream etc) or should i use express for this kind of features? (像pushStream等方法)还是我应该使用 express 来实现这种功能?

No. There is none so far.没有。到目前为止还没有。 But as long as you have the latest nodejs, you may use the http2 libraries.但是只要你有最新的 nodejs,你就可以使用 http2 库。

You have 2 options:您有 2 个选择:

  1. write a nestjs custom transport (I wonder no one has done that so far)编写一个nestjs 自定义传输(我想知道到目前为止没有人这样做过)
  2. mix it into your code yourself eg here a JS http2 client consuming自己将它混合到您的代码中,例如这里是一个 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()
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM