8000 Add http2 example · HowProgrammingWorks/NodeServer@6f9f245 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f9f245

Browse files
committed
Add http2 example
1 parent 8f4f38d commit 6f9f245

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

native-http2/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Start HTTP server
2+
3+
## With self-signed certificate (for testing)
4+
5+
- Generate certificates, run: `./cert/generate.sh`
6+
- Start server: `node server.js`
7+
- Open in browser: `https://127.0.0.1:8000/`
8+
9+
## With certbot (for production)
10+
11+
- Let's Encrypt is a free certificate authority: https://letsencrypt.org/
12+
- Use Certbot (free tool for automatically generatinging Let’s Encrypt
13+
certificates to enable HTTPS): https://certbot.eff.org/
14+
- Install: `dnf -y install certbot`
15+
- Generate certificate:
16+
`certbot certonly --standalone -d www.domain.com -d domain.com -m your.name@domain.com --agree-tos --no-eff-email`
17+
- Copy certificate:
18+
`cp /etc/letsencrypt/live/domain.com/fullchain.pem ./cert/cert.pem`
19+
- Copy private key:
20+
`cp /etc/letsencrypt/live/domain.com/privkey.pem ./cert/key.pem`
21+
- Or use your web server for challenge exchange:
22+
`certbot certonly --webroot -w ./ -d domain.com -m your.name@domain.com --agree-tos --no-eff-email`

native-http2/cert/generate.ext

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
authorityKeyIdentifier=keyid,issuer
2+
basicConstraints=CA:FALSE
3+
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
4+
subjectAltName = @alt_names
5+
6+
[alt_names]
7+
DNS.1 = example.com
8+
IP.1 = 127.0.0.1
9+
IP.2 = ::1

native-http2/cert/generate.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cd "$(dirname "$0")"
2+
openssl genrsa -out key.pem 3072
3+
openssl req -new -out self.pem -key key.pem -subj '/CN=localhost'
4+
openssl req -text -noout -in self.pem
5+
openssl x509 -req -days 1024 -in self.pem -signkey key.pem -out cert.pem -extfile generate.ext

native-http2/server.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
const fs = require('node:fs');
4+
const http2 = require('node:http2');
5+
6+
const user = { name: 'jura', age: 22 };
7+
8+
const routing = {
9+
'/': '<h1>welcome to homepage</h1><hr>',
10+
'/user': user,
11+
'/user/name': () => user.name.toUpperCase(),
12+
'/user/age': () => user.age,
13+
'/hello': { hello: 'world', andArray: [1, 2, 3, 4, 5, 6, 7] },
14+
'/api/method1': (stream, headers) => {
15+
console.log(headers[':path']);
16+
return { status: 200 };
17+
},
18+
'/api/method2': (stream, headers) => ({
19+
user,
20+
url: headers[':path'],
21+
cookie: headers[':cookie'],
22+
}),
23+
};
24+
25+
const types = {
26+
object: JSON.stringify,
27+
string: (s) => s,
28+
undefined: () => 'not found',
29+
function: (fn, stream, headers) => JSON.stringify(fn(stream, headers)),
30+
};
31+
32+
const key = fs.readFileSync('./cert/key.pem');
33+
const cert = fs.readFileSync('./cert/cert.pem');
34+
const options = { key, cert };
35+
36+
const server = http2.createSecureServer(options);
37+
38+
server.on('stream', (stream, headers) => {
39+
stream.respond({
40+
'content-type': 'text/html; charset=utf-8',
41+
':status': 200,
42+
});
43+
const path = headers[':path'];
44+
const data = routing[path];
45+
const type = typeof data;
46+
const serializer = types[type];
47+
const result = serializer(data, stream, headers);
48+
stream.end(result);
49+
});
50+
51+
server.listen(8000);
52+
console.log('Open: https://127.0.0.1:8000');
53+
54+
setInterval(() => user.age++, 2000);

0 commit comments

Comments
 (0)
0