Skip to content

Commit 0fe809c

Browse files
committed
Multidomain example
1 parent 68c6038 commit 0fe809c

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

native-multidomain/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-multidomain/cert/generate.ext

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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 = domain.com
8+
DNS.2 = localhost
9+
IP.1 = 127.0.0.1
10+
IP.2 = ::1

native-multidomain/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-multidomain/server.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
const fs = require('node:fs');
4+
const https = require('node:https');
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': (req, res) => {
15+
console.log(req.url + ' ' + res.statusCode);
16+
return { status: res.statusCode };
17+
},
18+
'/api/method2': (req) => ({
19+
user,
20+
url: req.url,
21+
cookie: req.headers.cookie
22+
}),
23+
};
24+
25+
const types = {
26+
object: JSON.stringify,
27+
string: (s) => s,
28+
undefined: () => 'not found',
29+
function: (fn, req, res) => JSON.stringify(fn(req, res)),
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 = https.createServer(options, (req, res) => {
37+
const data = routing[req.url];
38+
const type = typeof data;
39+
const serializer = types[type];
40+
const result = serializer(data, req, res);
41+
res.end(result);
42+
});
43+
44+
{
45+
const key = fs.readFileSync('./cert/key.pem');
46+
const cert = fs.readFileSync('./cert/cert.pem');
47+
const creds = { key, cert };
48+
server.addContext('127.0.0.1', creds);
49+
}
50+
51+
{
52+
const key = fs.readFileSync('./cert/key.pem');
53+
const cert = fs.readFileSync('./cert/cert.pem');
54+
const creds = { key, cert };
55+
server.addContext('localhost', creds);
56+
}
57+
58+
server.listen(8000);
59+
console.log('Open: https://127.0.0.1:8000');
60+
console.log(' or https://localhost:8000');
61+
62+
setInterval(() => user.age++, 2000);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy