TypeError: dest.end is not a function

TypeError: dest.end is not a function

我正在尝试使用 HTTP/2。我的express版本是5.0.0-alpha.2,http2版本是3.3.4.

我想 http2 应该 .

const http2 = require('http2');
// const http2 = require('spdy');  // using spdy package here, everything works perfect

const options = {
  key: fs.readFileSync(path.join(__dirname, 'private', 'server.key')),
  cert: fs.readFileSync(path.join(__dirname, 'private', 'server.crt'))
};

const server = http2
  .createServer(options, app)
  .listen(3000, err => {
    if (err) throw new Error(err);

    // I can see "Listening..." message, which means the server starts running well.
    console.log('Listening...');
  });

服务器启动 运行 很好,但是当我打开客户端网站时,它在终端中给我这个错误:

_stream_readable.js:512
    dest.end();
         ^

TypeError: dest.end is not a function
    at Stream.onend (_stream_readable.js:512:10)
    at Stream.g (events.js:286:16)
    at emitNone (events.js:91:20)
    at Stream.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:975:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

看来 node-http2 还没有被 Express 支持。 请在 github.

上跟踪此问题 Support for module http

在此期间,您可以留在node-spdy

const spdy = require('spdy');

const options = {
  key: fs.readFileSync(path.join(__dirname, 'private', 'server.key')),
  cert: fs.readFileSync(path.join(__dirname, 'private', 'server.crt'))
};

const server = spdy
  .createServer(options, app)
  .listen(3000, err => {
    if (err) throw new Error(err);
    console.log('Listening...');
  });

有了 Express 5.0,我们有了另一个解决方案:

express = require( 'express' ), //Web framework

// Solution 
express.request.__proto__ = http2.IncomingMessage.prototype;
express.response.__proto__ = http2.ServerResponse.prototype;

// Create app for server http/2
var apph2 = express();

这是服务器代码:

var
    application_root = __dirname,
    express     = require( 'express' ), //Web framework
    http2       = require('http2')
    logger      = require('morgan')
    fs          = require('fs')
    constants   = require('constants');


// Bunyan logger
var bunyan = require('bunyan');
var app = require('./apps/app_name');

var bunlog = bunyan.createLogger({name: "brqx_app"});


var credentials = {
//  log         : bunlog ,  
    key         : fs.readFileSync('/etc/letsencrypt/live/domain/privkey.pem'    ),
    cert        : fs.readFileSync('/etc/letsencrypt/live/domain/fullchain.pem'  ),
    ca          : fs.readFileSync("/etc/letsencrypt/live/domain/chain.pem"      ),
    dhparam     : fs.readFileSync("/etc/letsencrypt/archive/domain/dh1.pem"     ),
    secureOptions: constants.SSL_OP_NO_SSLv3 | constants.SSL_OP_NO_SSLv2
};

// Configure server

server = http2.createServer( credentials , app);

 server.listen(PORT , function () {
   console.log('Started Brqx http/2!');
} )

希望这些简单的台词对大家有所帮助。

当我们在网上搜索资料时,很重要的一点是测试代码的测试日期:2017 年 - 10 月。

此致。

Ricardo/Brqx.