-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
37 lines (32 loc) · 824 Bytes
/
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
var http = require('http');
var fs = require('fs');
var mime = require('mime');
var router = require('./router');
function handleRequest(req, res) {
req.pause();
var url = req.url;
if (url == '/') {
url = '/index.html';
}
var filePath = '.' + url;
fs.exists(filePath, function (exists) {
req.resume();
if (exists) {
fs.readFile(filePath, function (err, data) {
if (err) {
res.writeHead(500);
res.end();
}
console.log("200: " + url);
res.writeHead(200, {'Content-Type': mime.lookup(filePath)});
res.end(data);
});
} else {
router.route(req, res);
}
});
}
http.createServer(function (req, res) {
handleRequest(req, res);
}).listen(3000, "localhost");
console.log("server running at http://localhost:3000");