-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
40 lines (33 loc) · 821 Bytes
/
index.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
'use strict'
var http = require('http');
var fs = require('fs');
var path = require('path');
http.createServer(function(req, res) {
var filePath = '.' + req.url;
if (filePath === './') filePath = './test/index.html';
var extname = path.extname(filePath);
var contentType;
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
default:
contentType = 'text/html';
}
fs.readFile(filePath, function(error, content) {
if (error) {
if (error.code === 'ENOENT') {
res.writeHead(404);
} else {
res.writeHead(500);
}
res.end();
} else {
res.writeHead(200, {'Content-Type': contentType});
res.end(content, 'utf-8');
}
});
}).listen(5000);