Compatibility for the express module to work with the http2 module
npm install express-http2-workaround
npm install express-http2-workaround --save
`
Or download the latest release, or git clone the repository on GitHub.
$3
Important: When using the http2 module within NodeJS, use a path that resolves directly to the installed module, so it doesn't auto-resolve to the NodeJS internal http2 module. Issue #3
`javascript
var http2 = require('./node_modules/http2'); // Important: require the http2 module instead of NodeJS's internal http2 module
`
When this workaround is required, a function is available to call with an object as the argument that must contain the express module and the http2 module. If you want the express middleware to be automatically attached to the express application, simply pass the application as 'app' (see Method 1 below).
Method 1: (recommended for the average use-case)
`javascript
require('express-http2-workaround')({ express:express, http2:http2, app:expressApp });
`
Method 2:
`javascript
var expressHTTP2Workaround = require('express-http2-workaround');
expressApp.use(expressHTTP2Workaround({ express:express, http2:http2 }));
`
Method 3:
`javascript
var expressHTTP2WorkaroundMiddleware = require('express-http2-workaround')({ express:express, http2:http2 });
expressApp.use(expressHTTP2WorkaroundMiddleware);
// If you want to access an internal method, or overwrite a method for the module, see the advanced use in the readme
`
It is best to have this middleware added to your express application before any other middleware. Otherwise the known express+http2 issue may occur in previous middlewares for HTTP/2 requests.
This must be done for all sub express applications too.
`javascript
// let 'expressApp' be an existing express application
var expressHTTP2WorkaroundMiddleware = require('express-http2-workaround')({ express:express, http2:http2 }); // Create Middleware
expressApp.use(expressHTTP2WorkaroundMiddleware); // Set middleware on main express app
var subApp = express(); // create new sub express app
subApp.use(expressHTTP2WorkaroundMiddleware); // Set middleware on sub express app
expressApp.use(subApp); // Add the sub express app to the main/parent express app
`
Example
`javascript
// Require Modules
var fs = require('fs');
var express = require('express');
var http = require('http');
var http2 = require('./node_modules/http2'); // Important: require the http2 module instead of NodeJS's internal http2 module
// Create Express Application
var app = express();
// Make HTTP2 work with Express (this must be before any other middleware)
require('express-http2-workaround')({ express:express, http2:http2, app:app });
// Setup HTTP/1.x Server
var httpServer = http.Server(app);
httpServer.listen(80,function(){
console.log("Express HTTP/1 server started");
});
// Setup HTTP/2 Server
var httpsOptions = {
'key' : fs.readFileSync(__dirname + '/keys/ssl.key'),
'cert' : fs.readFileSync(__dirname + '/keys/ssl.crt'),
'ca' : fs.readFileSync(__dirname + '/keys/ssl.crt')
};
var http2Server = http2.createServer(httpsOptions,app);
http2Server.listen(443,function(){
console.log("Express HTTP/2 server started");
});
// Serve some content
app.get('/', function(req,res){
res.send('Hello World! Via HTTP '+req.httpVersion);
});
`
Advanced Use
The middleware returned by require('express-http2-workaround')({ express:express, http2:http2 }) is a function usable by express. It's a wrapper which calls the internal middleware method below. It also has the .instance property set to the instance object which have the following properties and methods:
requestHTTP2 - The new express request object which has [[Prototype]] set to http2.IncomingMessage.prototype. This object is unique per instance.
responseHTTP2 - The new express request object which has [[Prototype]] set to http2.ServerResponse.prototype. This object is unique per instance.
middleware - The internal middleware function which checks if the request is HTTP2, then calls setRequestAsHTTP2 and setResponseAsHTTP2.
setRequestAsHTTP2 - The function which sets the request object [[Prototype]] to requestHTTP2, it also locks it via Object.defineProperty to prevent sub express applications overwriting it.
setResponseAsHTTP2 - The function which sets the response object [[Prototype]] to responseHTTP2, it also locks it via Object.defineProperty to prevent sub express applications overwriting it.
For example, to overwrite a property or method, redefine it on .instance:
`javascript
var expressHTTP2WorkaroundMiddleware = require('express-http2-workaround')({ express:express, http2:http2 });
expressApp.use(expressHTTP2WorkaroundMiddleware);
// Log to console each time the middleware is called
var boundMiddleware = expressHTTP2WorkaroundMiddleware.instance.middleware.bind(expressHTTP2WorkaroundMiddleware.instance);
expressHTTP2WorkaroundMiddleware.instance.middleware = function(req, res, next){
console.log('Hello World!');
boundMiddleware(req, res, next);
};
`
The [[Prototype]] of the middleware is also set to the instance object, but please use .instance instead.
Tests
View results on [Travis-CI][travis-url], or run tests manually:
Install development dependencies for this module: npm install
Then run the test npm script: npm test`