Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: dest.end is not a function

I am trying to use HTTP/2. My express version is 5.0.0-alpha.2, http2 version is 3.3.4.

I suppose http2 should work well with express 5.

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...');
  });

The server starts running well, but when I open client website, it gives me this error in the terminal:

_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)
like image 904
Hongbo Miao Avatar asked Oct 19 '22 03:10

Hongbo Miao


2 Answers

It seems node-http2 has not been supported by Express yet. Please track this issue Support for module http on github.

In the meanwhile, you can stay with 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...');
  });
like image 135
Hongbo Miao Avatar answered Oct 20 '22 23:10

Hongbo Miao


With Express 5.0 we have another solution :

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();

And this is the server code :

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!');
} )

I hope these easy lines helps to people.

One thing is important when we search information on Internet is the date of test when code was tested : 2017 - October.

Regards.

Ricardo/Brqx.

like image 28
Ricardo Cabello Torres - Brqx Avatar answered Oct 21 '22 00:10

Ricardo Cabello Torres - Brqx