-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-ssl.js
More file actions
46 lines (36 loc) · 1.77 KB
/
server-ssl.js
File metadata and controls
46 lines (36 loc) · 1.77 KB
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
import { fileURLToPath } from 'url';
import { join, extname as _extname, dirname } from 'path';
import { createServer as createServerHTTPS } from 'https';
import { STATE } from './ssl/state.js';
import { Api, Endpoint } from 'simple-api-router';
STATE.importRequiredArguments(dirname(fileURLToPath(import.meta.url)));
const API = new Api("/api/");
const CONTENT_TYPES = {
'.css': 'text/css',
'.js': 'text/javascript',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
};
API.addEndpoint(new Endpoint("time", "GET", (req, res) => {
return STATE.respondWithContent(res, Date.now().toString(), STATE.TEXT_HTML);
}));
const HTTPS_SERVER = createServerHTTPS(STATE.loadDefaultSecureContext(), (req, res) => {
//const host = req.headers.host; // e.g., api.example.com
let route = undefined;
if (req.url === STATE.WEBSITE_ROOT) {
route = STATE.optEntry;
}
else if (API.checkFindExecute(req, res)) {
return;
}
route == undefined && (route = req.url); // no route, follow the url
STATE.defaultFileHandling(res, STATE.finishRoute(route), CONTENT_TYPES);
}).on('error', (e) => e.code === STATE.ADDR_IN_USE && console.error(`${STATE.optPort}${STATE.IN_USE}`)).listen(STATE.optPort, (err) => err ? console.error(STATE.ERROR_STARTING, err) : console.log(`${STATE.STARTED_HTTPS}${STATE.optPort}`));
STATE.startHttpChallengeListener(); // Lets Encrypt! HTTP-01 ACME Challenge Mixin - Always Redirects HTTP to HTTPS unless doing a ACME Challenge
STATE.loadLetsEncryptAcmeDaemon(() => { STATE.loadNewSecureContext(HTTPS_SERVER); });
// ^^ Update Certificates Callback
STATE.checkNodeForUpdates(); // Check Node.js version