-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
46 lines (41 loc) · 1.43 KB
/
app.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
const http = require('http');
const fs = require('fs');
const url = require('url');
const path = require('path');
let server = http.createServer(function (req, res) {
var pathname = __dirname + url.parse(req.url).pathname;
if (path.extname(pathname) == "") {
pathname += "/";
}
if (pathname.charAt(pathname.length - 1) == "/") {
pathname += "index.html";
}
switch (path.extname(pathname)) {
case ".html":
res.writeHead(200, { "Content-Type": "text/html" });
break;
case ".js":
res.writeHead(200, { "Content-Type": "text/javascript", "CaChe-Control": "max-age=691200" });
break;
case ".css":
res.writeHead(200, { "Content-Type": "text/css" });
break;
case ".gif":
res.writeHead(200, { "Content-Type": "image/gif", "CaChe-Control": "max-age=691200" });
break;
case ".jpg":
res.writeHead(200, { "Content-Type": "image/jpeg", "CaChe-Control": "max-age=691200" });
break;
case ".png":
res.writeHead(200, { "Content-Type": "image/png", "CaChe-Control": "max-age=691200" });
break;
default:
res.writeHead(200, { "Content-Type": "application/octet-stream" });
}
fs.readFile(pathname, function (err, data) {
res.end(data);
});
});
server.listen(80, () => {
console.log('server at port 80');
});