-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.js
More file actions
52 lines (47 loc) · 1.3 KB
/
Copy pathroutes.js
File metadata and controls
52 lines (47 loc) · 1.3 KB
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
const url = require('url');
const path = require('path');
const fs = require('fs');
const MimeType = require('./mime.js');
function routes(req, res, webRoot, logCallback) {
switch (req.method) {
case 'GET': {
const url = req.url;
let pathName = path.join(webRoot, url);
let resource = /[^\\]*$/.exec(pathName)[0];
if (resource === '') {
pathName = path.join(pathName, '/index.html');
}
fs.access(pathName, (err) => {
if (!err) {
let ext = path.parse(pathName).ext;
let type = MimeType(ext);
fs.readFile(pathName, 'utf8', (err, data) => {
if (!err) {
res.writeHead(200, {'Content-Type': type});
res.end(data);
logCallback(`200 ${req.method} => ${pathName}`);
} else {
res.writeHead(500, {'Content-Type': 'text/html'});
res.end('<h1>Internal Server Error</h1>');
logCallback(`500 ${req.method} => ${pathName}`);
}
});
} else {
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<h1>Not found</h1>');
logCallback(`404 ${req.method} => ${pathName}`);
}
});
break;
}
/*case 'HEAD': {
break;
}*/
default: {
res.writeHead(501, {'Content-Type': 'text/html'});
res.end('<h1>Not Implemented</h1>');
logCallback(`501 ${req.method}`);
}
}
}
module.exports = routes;