[Edit]
+
0
-
0

Express.js - run server using HTTP2 (h2 or h2c)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
// Simaple steps: // // 1. Create project directory and paste `index.js` into. // // 2. Generate example localhost certificate using: // openssl.exe req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj '/CN=localhost' // // Source: https://dirask.com/posts/openssl-generate-localhost-pem-certificate-under-Windows-1enOWD // // 3. Paste generated key.pem and cert.pem files directly into project directory. // // 4. Install dependencies using: // npm install // // 5. Run application using: // node index.js // // 6. Open in the web browser the following address: // https://localhost:8443 // -------------------------------------------------- // index.js file: // -------------------------------------------------- const fs = require('fs'); const path = require('path'); const http2 = require('http2'); const express = require('express'); // npm install express const bridge = require('http2-express-bridge'); // npm install http2-express-bridge const credentials = { key: fs.readFileSync(path.join(__dirname, 'key.pem'), 'utf8'), cert: fs.readFileSync(path.join(__dirname, 'cert.pem'), 'utf8'), }; const app = bridge(express); // ... app.get('/', (request, response) => { response.send('Hello World!'); }); // ... const server = http2.createSecureServer(credentials, app); server.listen(8443, () => console.log('Server started on https://localhost:8443')); // See also: // // 1. https://dirask.com/snippets/Express-js-run-server-using-HTTP2-h2-or-h2c-p5ddRD