-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (52 loc) · 1.68 KB
/
server.js
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
56
57
58
59
60
61
62
63
const http = require("node:http");
const https = require('node:https');
const files = require('./utils/files.js');
const server = {};
const app = {
nodeEnv: 'dev',
http: {
host: '0.0.0.0',
showHost: 'localhost',
port: 3002,
externalPort: 3002,
},
https: {
host: '0.0.0.0',
showHost: 'localhost',
port: 4432,
externalPort: 4432,
},
};
app.http.url = `http://${app.http.showHost}:${app.http.externalPort}`;
app.https.url = `https://${app.https.showHost}:${app.https.externalPort}`;
const requestListener = function (req, res) {
app.req = req;
app.res = res;
app.filePath = files.getFileFromUrl(req);
app.type = req.socket.localPort == app.http.port ? 'http' : 'https';
// Get the original URL from the request
app.originalUrl = `${app.type}://${req.headers.host}${req.url}`;
console.log(`Request ${req.method} for ${app.filePath} on ${app.type} at ${app.originalUrl}`);
if (req.method === 'POST') {
files.saveData(app);
} else {
if (app.filePath === '/') {
files.renderHomePage(app);
} else {
files.render(app);
}
}
}
server.http = http.createServer(requestListener);
server.http.listen(app.http.port, app.http.host, () => {
console.log(`\nServer is running on [${app.nodeEnv}] ${app.http.url}`);
});
// self-sign by using cert.pem instead of obtaining a ca.pem for the real world
server.https = https.createServer({
key: files.readFileSync('_keys/key.pem'),
cert: files.readFileSync('_keys/cert.pem'),
ca: files.readFileSync('_keys/cert.pem')
}, requestListener);
server.https.listen(app.https.port, app.https.host, () => {
console.log(`\nServer is running on https [${app.nodeEnv}] ${app.https.url}`);
});