-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
31 lines (25 loc) · 846 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
import * as fs from "node:fs";
import * as http from "node:http";
import * as path from "node:path";
export default function devserver(routes) {
if (!routes) {
console.log('Please check option: routes !!');
}
const folders = routes;
const server = http.createServer((req, res) => {
const foundFolder = folders.find((folder) => req.url.startsWith(folder.route));
if (foundFolder) {
const filePath = path.join(foundFolder.path, req.url.replace(foundFolder.route, ''));
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
const indexPath = path.join(filePath, 'index.html');
if (fs.existsSync(indexPath)) {
fs.createReadStream(indexPath).pipe(res);
return;
}
}
}
res.statusCode = 404;
res.end('Not found');
});
return server;
}