-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
45 lines (40 loc) · 1.19 KB
/
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
38
39
40
41
42
43
44
45
/**
* Recieve log message sent by JS Error Logger and dump it to console
*/
var http = require('http')
, fs = require('fs')
, path = require('path')
, util = require('util');
http.createServer(function(req, res) {
var input
, file = req.url == '/' ? 'demo.html' : path.relative('/', req.url)
, ext = {
html: "text/html",
css: "text/css",
js: "application/javascript"
};
if (req.method === 'GET') {
fs.exists(file, function(exists) {
if (exists) {
res.writeHead(200, {'Content-Type': ext[path.extname(file).slice(1)]});
console.log(req.method + ' ' + req.url + '\t' + res.statusCode);
fs.readFile(file, function(err, data) {
if (err) throw err;
res.end(data);
});
} else {
res.statusCode = 404;
console.log(req.method + ' ' + req.url + '\t' + res.statusCode);
res.end();
}
});
} else {
req.on('data', function(chunk) {
input ? (input += chunk) : (input = chunk);
});
req.on('end', function() {
console.log(util.inspect(JSON.parse(input), false, 4));
});
}
}).listen(3000, '127.0.0.1');
console.log('Ready on http://127.0.0.1:3000/');