-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
35 lines (29 loc) · 879 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
'use strict';
var http = require('http'),
path = require('path'),
fs = require('fs');
http.createServer(function (req, res) {
var type = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'text/javascript'
};
if (req.method.toLowerCase() === 'get') {
var file = path.join(__dirname, req.url === '/' ? '/index.html' : req.url);
fs.readFile(file, function (err, body) {
if (err) {
res.writeHead(404);
res.end();
return;
}
res.writeHead(200, {
'Content-Length': body.length,
'content-type': type[path.extname(file)] || 'text/plain'
});
res.end(body);
});
return;
}
res.writeHead(404);
res.end();
}).listen(3000);