I am creating an api with nodejs and express and I want to integrate http2 with ExpressJS
This is my code:
'use strict';
const http2 = require('http2');
const fs = require('fs');
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 443;
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Routes variables
const indexRouter = require('./routes/index');
// Routes uses
app.use('/', indexRouter);
// Server configurations
const key = path.join(__dirname + '/security/key.pem');
const cert = path.join(__dirname + '/security/certificate.pem');
const options = {
key: fs.readFileSync(key),
cert: fs.readFileSync(cert)
}
const server = http2.createSecureServer(options, app);
server.on('error', err => console.log(err));
server.listen(port, () => {
console.log('Server running')
})
I am trying to pass express server as second parameter of createSecureServer() but I am not sure if I am right with this, cause I am getting this error:
[nodemon] 2.0.2 [nodemon] to restart at any time, enter
rs
[nodemon] watching dir(s): . [nodemon] watching extensions: js,mjs,json [nodemon] startingnode index.js
_http_incoming.js:96 if (this.socket.readable) ^TypeError: Cannot read property 'readable' of undefined at IncomingMessage._read (_http_incoming.js:96:19) at IncomingMessage.Readable.read (stream_readable.js:491:10) at resume (_stream_readable.js:976:12) at processTicksAndRejections (internal/process/task_queues.js:80:21) [nodemon] app crashed - waiting for file changes before starting...
It should be noted that my certificate, although self-signed and unreliable, is loading correctly. I try not to use a third-party module if I can do it with NodeJS. Any help?