<commit_before># express-http2-workaround - Nodejs Module
> Let HTTP2 work with express

[![NPM Version][npm-image]][npm-url]
[![Downloads Stats][npm-downloads]][npm-url]
[![Travis CI][travis-image]][travis-url]

Compatibility for the [express](https://www.npmjs.com/package/express) module to work with the [http2](https://www.npmjs.com/package/http2) module

## What is this?

The awesome [http2](https://www.npmjs.com/package/http2) nodejs module (https://github.com/molnarg/node-http2) does not work straight out the box with the awesome [express](https://www.npmjs.com/package/express) module (https://github.com/expressjs/express).

Due to express having the request and response objects [[Prototype]] the inbuilt nodejs http  IncomingMessage and ServerResponse objects, all requests served by express that are initialised by something else, such as http2, cause an error.

This issue is mentioned in many places:
https://github.com/expressjs/express/issues/2364 ,
https://github.com/molnarg/node-http2/issues/220 ,
https://github.com/molnarg/node-http2/issues/100 

This module creates new express request and response objects, then sets their [[Prototype]] to http2 IncomingMessage and ServerResponse objects.
The middleware returned by this module simply checks if the connection is http2 and sets the request and response [[Prototype]] to the newly created ones which have the http2 [[Prototype]].

Sadly, setting [[Prototype]] effects performance<sup>[[1]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto)[[2]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf)</sup>, which not much can be done about until a better fix/workaround is thought of, or until express changes the way their request and response objects are handled.

## Why use this?

At the moment, if someone wants to create an express application that has HTTP/2, while also serving HTTP/1.x, the closest thing you can use is the [spdy](https://www.npmjs.com/package/spdy) module which works well with [express](https://www.npmjs.com/package/express).

With this module, you can use HTTP/2 via the [http2](https://www.npmjs.com/package/http2) module with [express](https://www.npmjs.com/package/express).

## News:
Nodejs's experimental http2 is now available in Node v8.4.0+ under the flag "--expose-http2". See https://github.com/nodejs/node/pull/14239
This module does not support the new implementation. Hopefully express adds support.

## Installation

Install the module via [NPM](https://www.npmjs.com/package/express-http2-workaround)
```
npm install express-http2-workaround --save
```
Or [download the latest release](https://github.com/Unchosen/express-http2-workaround/releases), or git clone the [repository on GitHub](https://github.com/Unchosen/express-http2-workaround).


### How to use

When 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<commit_msg>Improve Readme<commit_after># express-http2-workaround - Nodejs Module
> Let HTTP2 work with express

[![NPM Version][npm-image]][npm-url]
[![Downloads Stats][npm-downloads]][npm-url]
[![Travis CI][travis-image]][travis-url]

Compatibility for the [express](https://www.npmjs.com/package/express) module to work with the [http2](https://www.npmjs.com/package/http2) module

## What is this?

The awesome [http2](https://www.npmjs.com/package/http2) nodejs module (https://github.com/molnarg/node-http2) does not work straight out the box with the awesome [express](https://www.npmjs.com/package/express) module (https://github.com/expressjs/express).

Due to express having the request and response objects [[Prototype]] the inbuilt nodejs http  IncomingMessage and ServerResponse objects, all requests served by express that are initialised by something else, such as http2, cause an error.

This issue is mentioned in many places:
https://github.com/expressjs/express/issues/2364 ,
https://github.com/molnarg/node-http2/issues/220 ,
https://github.com/molnarg/node-http2/issues/100 

This module creates new express request and response objects, then sets their [[Prototype]] to http2 IncomingMessage and ServerResponse objects.
The middleware returned by this module simply checks if the connection is http2 and sets the request and response [[Prototype]] to the newly created ones which have the http2 [[Prototype]].

Sadly, setting [[Prototype]] effects performance<sup>[[1]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto)[[2]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf)</sup>, which not much can be done about until a better fix/workaround is thought of, or until express changes the way their request and response objects are handled.

## Why use this?

At the moment, if someone wants to create an express application that has HTTP/2, while also serving HTTP/1.x, the closest thing you can use is the [spdy](https://www.npmjs.com/package/spdy) module which works well with [express](https://www.npmjs.com/package/express).

With this module, you can use HTTP/2 via the [http2](https://www.npmjs.com/package/http2) module with [express](https://www.npmjs.com/package/express).

## News:
Nodejs's experimental http2 is now available in Node v8.4.0+ under the flag "--expose-http2". See https://github.com/nodejs/node/pull/14239

This module does not support the new implementation. Hopefully express adds support.

## Installation

Install the module via [NPM](https://www.npmjs.com/package/express-http2-workaround)
```
npm install express-http2-workaround --save
```
Or [download the latest release](https://github.com/Unchosen/express-http2-workaround/releases), or git clone the [repository on GitHub](https://github.com/Unchosen/express-http2-workaround).


### How to use

When 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
{'fraction_non_alphanumeric': 0.11400052534804307, 'fraction_numerical': 0.016285789335434727, 'mean_word_length': 5.309030654515327, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 38, 'lorem ipsum': 0, 'www.': 18, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6357178', 'n_tokens_mistral': 2228, 'n_tokens_neox': 2124, 'n_words': 752}