-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (40 loc) · 1.09 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
47
48
49
50
51
52
53
54
let fs = require('fs');
let path = require('path');
let http = require('http');
let basePath = __dirname;
var staticServe = function(req, res) {
let filePath = path.resolve(basePath);
filePath = path.join(filePath, req.url);
if (!fs.existsSync(filePath)) {
res.writeHead(404, 'Not Found');
res.write("File: '"+ filePath + "' doesn't exist.");
res.end();
}
let type=fs.lstatSync(filePath);
if (!type.isFile()) {
res.writeHead(404, 'Not Found');
res.write("Requested path is directory or link.");
res.end();
}
var fileExt=path.extname(filePath)
let content= 'text/html; charset=utf-8'
switch(fileExt) {
case '.json':
content='application/json; charset=utf-8'
break;
case '.jpeg':
content='image/jpeg'
break;
case '.js':
content='application/javascript'
break;
}
res.setHeader('Content-Type', content);
fs.readFile(filePath ,{flags: 'r', encoding: "utf8"}, function (err, data) {
res.writeHead(200, "ok");
res.write(data);
res.end();
});
};
var httpServer = http.createServer(staticServe);
httpServer.listen(8081);