【问题标题】:Using HTTP2 directly in NestJS在 NestJS 中直接使用 HTTP2
【发布时间】:2019-11-25 06:30:16
【问题描述】:

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

【问题讨论】:

    标签: javascript nestjs


    【解决方案1】:

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

    你有两个选择:

    1. 写一个nestjs custom transport(我不知道到目前为止没有人这样做)
    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()
    })
    

    【讨论】:

      猜你喜欢
      • 2021-08-26
      • 2022-01-08
      • 2018-03-07
      • 2017-03-17
      • 2016-08-23
      • 2020-12-20
      • 2023-03-05
      • 1970-01-01
      • 2019-06-18
      相关资源
      最近更新 更多