-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathserver.js
50 lines (40 loc) · 1.02 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
"use strict";
var path = require("path");
var http = require("http");
var nodeStaticAlias = require("node-static-alias");
const PORT = 8049;
const WEB_DIR = path.join(__dirname,"web");
var httpServer = http.createServer(handleRequest);
var staticServer = new nodeStaticAlias.Server(WEB_DIR,{
serverInfo: "Web Workers",
cache: 1
});
httpServer.listen(PORT);
console.log(`Server started on http://localhost:${PORT}...`);
// *******************************
async function handleRequest(req,res) {
// handle static file requests
if (["GET","HEAD"].includes(req.method)) {
// special handling for empty favicon
if (req.url == "/favicon.ico") {
res.writeHead(204,{
"Content-Type": "image/x-icon",
"Cache-Control": "public, max-age: 604800"
});
res.end();
return;
}
// handle other static files
staticServer.serve(req,res,function onStaticComplete(err){
if (err) {
res.writeHead(404);
res.end();
}
});
}
// Oops, invalid/unrecognized request
else {
res.writeHead(404);
res.end();
}
}