-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
95 lines (82 loc) · 2.57 KB
/
server.js
File metadata and controls
95 lines (82 loc) · 2.57 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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const fs = require('fs');
const http = require('http');
const path = require('path');
const { directories: dirs, notFoundFile } = require('./config.js');
const watchContent = require('./helpers/fileWatchers.js');
const build = require('./build.js');
const { WebSocket } = require('ws');
const PORT = process.env.PORT || 3000;
const MIME_TYPES = {
default: 'application/octet-stream',
html: 'text/html; charset=UTF-8',
js: 'application/javascript',
css: 'text/css',
png: 'image/png',
jpg: 'image/jpg',
gif: 'image/gif',
ico: 'image/x-icon',
svg: 'image/svg+xml',
};
const fileExists = async filePath => {
try {
await fs.promises.access(filePath);
return true;
} catch {
return false;
}
};
const getMimeType = ext => MIME_TYPES[ext] || MIME_TYPES.default;
const prepareFile = async url => {
// Create a new URL object (base URL is required, localhost can be a placeholder)
const myURL = new URL(url, 'http://localhost');
// Use the pathname from the URL object
let filePath = path.join(
dirs.dist,
myURL.pathname.endsWith('/') ? 'index.html' : myURL.pathname
);
if (!path.extname(filePath) && (await fileExists(`${filePath}.html`))) {
filePath += '.html';
}
const isPathTraversal = !filePath.startsWith(dirs.dist);
const found = !isPathTraversal && (await fileExists(filePath));
return { found, filePath };
};
const startServer = async () => {
await build();
const server = http
.createServer(async (req, res) => {
try {
const { found, filePath } = await prepareFile(req.url);
const streamPath = found
? filePath
: path.join(dirs.dist, notFoundFile || '404.html');
const ext = path.extname(streamPath).substring(1).toLowerCase();
const mimeType = getMimeType(ext);
res.writeHead(found ? 200 : 404, { 'Content-Type': mimeType });
const stream = fs.createReadStream(streamPath);
stream.pipe(res);
stream.on('error', err => {
console.error('Stream error', err);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal server error');
});
} catch (error) {
console.error('Request handling error', error);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal server error');
}
})
.listen(PORT, () => {
console.log(
`\x1b[32mServer running at http://localhost:${PORT}/\x1b[0m`
);
});
const wss = new WebSocket.Server({ noServer: true });
server.on('upgrade', (request, socket, head) => {
wss.handleUpgrade(request, socket, head, ws => {
wss.emit('connection', ws, request);
});
});
await watchContent(wss);
};
module.exports = startServer;