├── LICENSE ├── README.md ├── certificate ├── server.crt ├── server.csr └── server.key ├── express ├── README.md ├── index.js └── package.json ├── hapi ├── README.md ├── index.js └── package.json └── koa ├── README.md ├── index.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Ivan Jovanovic 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Express, Koa and Hapi on HTTP2 2 | 3 | This repo has examples for post: http://ivanjov.com/running-express-koa-and-hapi-on-http-2/ 4 | 5 | Each folder has readme file for running code in it. List of folders: 6 | 7 | 1. [Express](https://github.com/IvanJov/node-on-http2/tree/master/express) 8 | 2. [Koa](https://github.com/IvanJov/node-on-http2/tree/master/koa) 9 | 3. [Hapi](https://github.com/IvanJov/node-on-http2/tree/master/hapi) 10 | -------------------------------------------------------------------------------- /certificate/server.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE-----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euQhm9YdqnLdQZgpDFfHDsmLCayVxT4KGK27fgDQ 18 | NVDfq0APk31xFXTDGgXjdOLO3CxVK2nIPjivL6xTVhvO+Zg7 19 | -----END CERTIFICATE----- 20 | -------------------------------------------------------------------------------- /certificate/server.csr: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE REQUEST----- 2 | MIICrDCCAZQCAQAwUjELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWEx 3 | CzAJBgNVBAcTAlNGMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQw 4 | ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCxTLEitFys8u45eew77LXT 5 | X335Jiv62FE2VbnkjKpvkVBLWmx8pDrOEJftGzyv9H6N1T/MBlVOwji1rYaRDXuN 6 | opH7zEbK2n4qA/hb690cdObrplSCd8AL8fjLChl9auptB78aqBIbemZBKS6A0pQc 7 | rBTwch0OycJeC8qsXGOfJ4y5hSruIH1XChk/LbQ/PPsncKkU4I9a+0DaC27l0WnM 8 | VEdvip97DAtuB/CruHHbMPi0zie0VyW/81/UFRosWgthNVDBVP8c0d2xD3SX3Cfw 9 | jvu6H+sGuQ6njVZyz2z2jsBed+4n19GDVNT7iPjC4UdTorFlbCfrhPOuqpozAmDr 10 | AgMBAAGgFTATBgkqhkiG9w0BCQcxBhMEdGVzdDANBgkqhkiG9w0BAQUFAAOCAQEA 11 | hB9V3eY5wSopuibMCn62fRt92kbM52cG4BQmwef15nNH9Zw5qwPMXuR8M2VvPell 12 | SkEB1pB4NfijVzi9Mc7THopGbr57MWtpdKEdMfPey2plstYvZ5IICyW3hf4j3tYM 13 | 3vy0ykVCfq9gxvX0PFqnGV1EhhN1viMhHrbYG1Dyo7lVp/oAzLtWuvBNNRSrP3Ul 14 | lB+vcPGKjTx4kiwHYm/xoVvVOi7PqgSf2HLyv/0E195eQD1F7KOamEaVazJ6PJv6 15 | nVwO6D76qz5pbsABduNH8wLdF6NC3QnDFg+qGiEY+OyOms1IZb+nwIDufjjdq1Ti 16 | AvifX4fvLahDYEzcs+knwA== 17 | -----END CERTIFICATE REQUEST----- 18 | -------------------------------------------------------------------------------- /certificate/server.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEowIBAAKCAQEAsUyxIrRcrPLuOXnsO+y10199+SYr+thRNlW55Iyqb5FQS1ps 3 | fKQ6zhCX7Rs8r/R+jdU/zAZVTsI4ta2GkQ17jaKR+8xGytp+KgP4W+vdHHTm66ZU 4 | gnfAC/H4ywoZfWrqbQe/GqgSG3pmQSkugNKUHKwU8HIdDsnCXgvKrFxjnyeMuYUq 5 | 7iB9VwoZPy20Pzz7J3CpFOCPWvtA2gtu5dFpzFRHb4qfewwLbgfwq7hx2zD4tM4n 6 | tFclv/Nf1BUaLFoLYTVQwVT/HNHdsQ90l9wn8I77uh/rBrkOp41Wcs9s9o7AXnfu 7 | J9fRg1TU+4j4wuFHU6KxZWwn64TzrqqaMwJg6wIDAQABAoIBAHotqVuLAtpDxhph 8 | hIN/CYWRppPM1D3WV5Cj3qg6rzCW6MgkleIGomdiU++W92khiRxgOch1DNBrUv0P 9 | iU3oBp6ByyvDh30+t/ZoKfveO87iWEdHpbEKqJMSuing2dPtm4Jq9ix+aYv5+wEo 10 | Sd2IP3zKsNtJJ/EK5CA+6/l+6T/AsGTbYD5Qzod3u9w+F07iEmy61tsnH2nSwcpz 11 | r7azJ899fqodwHdsMp3ejbO8a8sU8PhYGAwJjbr+EXplhBsTl67rBxxqpVu8Wj3y 12 | xDyL76S9m9NA+0tSybAzy/cfvyQW9Nx1pL5esh8KjP6x8kNUuduwftSfTLIyHNzW 13 | E+u8tcECgYEA6npJtjZIhWHVrVMcMiJUmNtVFuxcWAxyFUDkzoN9ZlahCcxdoGZB 14 | f28FuLu0Hmqs3csvocL4OgdkOlSRZP59m65IXV5jexbM9Ic3pfyBLLs1/lU1ubb0 15 | HQbY8sZc7pdQdLt5sLP0xEtb95wofz+B3CjC7F09f5DXZGoRRSf5TRsCgYEAwZLX 16 | +qqHMBu3Bvdzc2opTajTWHEbb8VVvJdXBeeIV5Cm7Y4nmMIp4/h40SHgTLsZBvas 17 | YtfGMMVzT8J57rMVXcLN/UOe6nZQPruv9OJkT36w0Gd71eN6aK7VWzYF32fzF4vx 18 | NQGud89uH20OI+OhGSdNCzCqcycXFuXlXWwTiHECgYEAzx5KMlvs33U2LC43v8k3 19 | dPZ5SRBr2ZLUyxVRh2H6yQVIpsERCviIbqqBIVccmamdHSW342m3nnqc/DFrZQyv 20 | o27lmSQhN79CN9WhQEu1Ru/vl8No+60yINnFlV3HBTI/7X0U1xN+6E2w5R3ZX92X 21 | NgQDeoo6Io6s1JQkwTO4MXUCgYBs/ia4EG6gKurKdGhAMyVggc/Ge6FMNNuKndg1 22 | DeIaE+u51JH9FE6L+yrj7mQiMDLuVdRIr1/7tCGyZjlLqvA74+UiVoHTWJzkRb/U 23 | bei3Tgl2vt3eWABjkxW1Dt0NprUcrAI7q1+2PEiwTzJoCNJ7nEJoK5W90uPQU8/q 24 | 42C4MQKBgDKyO65QPAG7yobGHhFPYnkjGPJLN2sH/P475J5HyL5AycTBIUqxYzX1 25 | DDRZxJ5IfyI9gbie3yXqIgaAvu81t8rbIJ37J03iyQ3AFUTUNACwie19jsc4BiZ6 26 | E3SU6chSX9vcYACBQoH3zy1iYe/96PmZ+TOybYK7cJkLrnrtgayY 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /express/README.md: -------------------------------------------------------------------------------- 1 | # How to run code locally? 2 | 3 | 1. Install dependencies with `npm install` 4 | 2. Run code with `node index` 5 | 3. Open [https://localhost:3000/](https://localhost:3000/) -------------------------------------------------------------------------------- /express/index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const spdy = require('spdy'); 3 | const fs = require('fs'); 4 | 5 | const app = express(); 6 | 7 | app.get('/', function (req, res) { 8 | res.send('Serving using HTTP2!'); 9 | }); 10 | 11 | const options = { 12 | key: fs.readFileSync(__dirname + '/../certificate/server.key'), 13 | cert: fs.readFileSync(__dirname + '/../certificate/server.crt') 14 | }; 15 | 16 | spdy 17 | .createServer(options, app) 18 | .listen(3000, (err) => { 19 | if (err) { 20 | throw new Error(err); 21 | } 22 | 23 | console.log('Listening on port: ' + 3000 + '.'); 24 | }); -------------------------------------------------------------------------------- /express/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "expressjs", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "MIT", 11 | "dependencies": { 12 | "express": "^4.14.0", 13 | "spdy": "^3.4.4" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /hapi/README.md: -------------------------------------------------------------------------------- 1 | # How to run code locally? 2 | 3 | 1. Install dependencies with `npm install` 4 | 2. Run code with `node index` 5 | 3. Open [https://localhost:3000/](https://localhost:3000/) -------------------------------------------------------------------------------- /hapi/index.js: -------------------------------------------------------------------------------- 1 | const hapi = require('hapi'); 2 | const http2 = require('http2'); 3 | const fs = require('fs'); 4 | 5 | const options = { 6 | key: fs.readFileSync(__dirname + '/../certificate/server.key'), 7 | cert: fs.readFileSync(__dirname + '/../certificate/server.crt') 8 | }; 9 | 10 | const server = new hapi.Server(); 11 | server.connection({ 12 | listener: http2.createServer(options), 13 | host: 'localhost', 14 | port: 3000, 15 | tls: true 16 | }); 17 | 18 | server.route({ 19 | method: 'GET', 20 | path:'/', 21 | handler: (req, res) => { 22 | 23 | return res('Serving using HTTP2!'); 24 | } 25 | }); 26 | 27 | server.start((err) => { 28 | if (err) { 29 | throw err; 30 | } 31 | console.log('Server running at:', server.info.uri); 32 | }); -------------------------------------------------------------------------------- /hapi/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hapijs", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "MIT", 11 | "dependencies": { 12 | "hapi": "^16.1.0", 13 | "http2": "^3.3.6" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /koa/README.md: -------------------------------------------------------------------------------- 1 | # How to run code locally? 2 | 3 | 1. Install dependencies with `npm install` 4 | 2. Run code with `node index` 5 | 3. Open [https://localhost:3000/](https://localhost:3000/) -------------------------------------------------------------------------------- /koa/index.js: -------------------------------------------------------------------------------- 1 | const http2 = require('http2'); 2 | const koa = require('koa'); 3 | const _ = require('koa-route'); 4 | const fs = require('fs'); 5 | 6 | const app = koa(); 7 | 8 | app.use(_.get('/', function *(next) { 9 | this.body = 'Serving using HTTP2!'; 10 | yield next; 11 | })); 12 | 13 | const options = { 14 | key: fs.readFileSync(__dirname + '/../certificate/server.key'), 15 | cert: fs.readFileSync(__dirname + '/../certificate/server.crt') 16 | }; 17 | 18 | http2 19 | .createServer(options, app.callback()) 20 | .listen(3000, (err) => { 21 | if (err) { 22 | throw new Error(err); 23 | } 24 | 25 | console.log('Listening on port: ' + 3000 + '.'); 26 | }); -------------------------------------------------------------------------------- /koa/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "koajs", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "MIT", 11 | "dependencies": { 12 | "http2": "^3.3.6", 13 | "koa": "^1.2.4", 14 | "koa-route": "^2.4.2" 15 | } 16 | } 17 | --------------------------------------------------------------------------------