-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
71 lines (56 loc) · 1.74 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
64
65
66
67
68
69
70
71
"use strict";
const https = require('https');
const fs = require('fs');
const url = require('url');
const path = require('path');
const options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
};
const MIME_TYPES = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.doc': 'application/msword'
};
const PORT = 8181;
function serveStatic(req, res) {
const parsedUrl = url.parse(req.url);
const parsedPath = `.${parsedUrl.pathname}`;
const isFolder = fs.statSync(parsedPath)?.isDirectory();
const pathname = `${parsedPath}${isFolder ? '/index.html' : ''}`;
const ext = !isFolder ? path.parse(pathname).ext : '.html';
const mime = MIME_TYPES[ext] || 'text/plain';
res.setHeader('Content-type', mime );
console.log(`${req.method} ${req.url} ${mime}`);
// read file from file system
fs.readFile(pathname, (err, data) => {
if (err) {
res.statusCode = 500;
res.setHeader('Content-type', 'text/plain' );
res.end(`Error getting the file: ${err}.`);
} else {
// if the file is found, set Content-type and send data
res.end(data);
}
});
}
const server = https.createServer(options, (req, res) => {
try {
serveStatic(req, res);
} catch (e) {
res.statusCode = 500;
res.end(`Error getting the file: ${e}.`);
}
});
server.listen(PORT, () => {
console.log(`Server listening on: https://localhost:${PORT}`);
});