-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.js
More file actions
38 lines (34 loc) · 1019 Bytes
/
hello.js
File metadata and controls
38 lines (34 loc) · 1019 Bytes
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
var http = require('http'),
fs = require('fs');
function serveStaticFile(res, path, contentType, responseCode){
if(!responseCode) responseCode = 200;
fs.readFile(__dirname + path, function(err, data){
console.log(__dirname + path);
console.log(err);
if (err){
res.writeHead(500, {'Content-Type': 'text-plain'});
res.end('500 - internal Error');
}
else{
res.writeHead(responseCode, {'Content-Type': contentType});
res.end(data);
}
});
}
http.createServer(function(req,res){
var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
switch (path) {
case '':
serveStaticFile(res, '/public/home.html', 'text/html');
break;
case '/about':
serveStaticFile(res, '/public/about.html', 'text/html');
break;
case '/img/js.png':
serveStaticFile(res, '/public/images/js.png', 'image/png');
break;
default:
serveStaticFile(res, '/public/404.html', 'text/html', 404);
break;
}
}).listen(3000);