Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js server and HTTP/2 (2.0) with express.js

Is it possible currently to get node.js HTTP/2 (HTTP 2.0) server? And http 2.0 version of express.js?

like image 668
WHITECOLOR Avatar asked Feb 20 '15 23:02

WHITECOLOR


People also ask

Does ExpressJS support http2?

express : Express is a Node. js framework for building web applications and backend APIs. spdy : spdy is an express compatible module that creates HTTP/2 enabled servers in Node. js.

What is difference between HTTP and Express in node JS?

HTTP is an independent module. Express is made on top of the HTTP module. HTTP module provides various tools (functions) to do things for networking like making a server, client, etc. Express along with what HTTP does provide many more functions in order to make development easy.

Does Node fetch support http2?

- `node-fetch` doesn't support HTTP/2 and has had an issue for support open since 2017: node-fetch/node-fetch#342 - `fetch-h2` supports HTTP1.

Is node JS and ExpressJS same?

Node. js is a platform for building the i/o applications which are server-side event-driven and made using JavaScript. Express. js is a framework based on Node.


2 Answers

var express = require('express'); var app = express();  app.get('/', function (req, res) {   res.send('hello, http2!'); });  var options = {   key: fs.readFileSync('./example/localhost.key'),   cert: fs.readFileSync('./example/localhost.crt') };  require('http2').createServer(options, app).listen(8080); 

EDIT

This code snippet was taken from a conversation on Github.

like image 96
HDK Avatar answered Sep 20 '22 14:09

HDK


If you are using express@^5 and http2@^3.3.4, then the correct way to start the server is:

const http2 = require('http2'); const express = require('express');  const app = express();  // app.use('/', ..);  http2     .raw     .createServer(app)     .listen(8000, (err) => {         if (err) {             throw new Error(err);         }          /* eslint-disable no-console */         console.log('Listening on port: ' + argv.port + '.');         /* eslint-enable no-console */     }); 

Notice the https2.raw. This is required if you want to accept TCP connections.

Note that at the time of this writing (2016 05 06), none of the major browsers support HTTP2 over TCP.

If you want to accept TCP and TLS connections, then you need to start the server using the default createServer method:

const http2 = require('http2'); const express = require('express'); const fs = require('fs');   const app = express();  // app.use('/', ..);  http2     .createServer({         key: fs.readFileSync('./localhost.key'),         cert: fs.readFileSync('./localhost.crt')     }, app)     .listen(8000, (err) => {         if (err) {             throw new Error(err);         }          /* eslint-disable no-console */         console.log('Listening on port: ' + argv.port + '.');         /* eslint-enable no-console */     }); 

Note that at the time of this writing, I did manage to make express and http2 to work (see https://github.com/molnarg/node-http2/issues/100#issuecomment-217417055). However, I have managed to get http2 (and SPDY) to work using spdy package.

const spdy = require('spdy'); const express = require('express'); const path = require('path'); const fs = require('fs');   const app = express();  app.get('/', (req, res) => {     res.json({foo: 'test'}); });  spdy     .createServer({         key: fs.readFileSync(path.resolve(__dirname, './localhost.key')),         cert: fs.readFileSync(path.resolve(__dirname, './localhost.crt'))     }, app)     .listen(8000, (err) => {         if (err) {             throw new Error(err);         }          /* eslint-disable no-console */         console.log('Listening on port: ' + argv.port + '.');         /* eslint-enable no-console */     }); 
like image 32
Gajus Avatar answered Sep 18 '22 14:09

Gajus