-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
33 lines (25 loc) · 878 Bytes
/
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
const { existsSync, createReadStream } = require('fs'),
{ join } = require('path');
const SERVER_PORT = 88,
SERVER_DIR = join(__dirname, process.argv[2]),
SPA_HOME_PAGE = SERVER_DIR + '/index.html';
const ans = (res, url) => {
console.log(`<-- Found: ${url}\n`);
createReadStream(url).pipe(res);
};
require('http').createServer((req, res) => {
const { url } = req;
console.log(`--> Route: ${url}`);
if (url.match(/^\/(?:dist|node_modules)\//)) {
ans(res, __dirname + url);
} else if (url.match(/^\/\w*$/)) {
ans(res, SPA_HOME_PAGE);
} else if (existsSync(SERVER_DIR + url)) {
ans(res, SERVER_DIR + url);
} else {
res.writeHead(404);
res.end();
console.log('<-- Not found.\n');
}
}).listen(SERVER_PORT);
console.log(`Server started on ${SERVER_PORT} in "${SERVER_DIR}".\n`);