-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (41 loc) · 1.34 KB
/
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
41
42
43
44
45
46
47
48
49
var http = require('http');
var fs = require('fs');
var path = require('path');
var server = http.createServer(function(request, response){
var filePath = './public' + request.url;
if (filePath == './public/')
filePath = './public/index.html';
console.log("serving ", filePath);
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.css':
contentType = 'text/css';
break;
case '.json':
case '.png':
contentType = 'image/png';
break;
}
fs.readFile(filePath, function(error, content) {
if (error) {
if(error.code == 'ENOENT'){
fs.readFile('./404.html', function(error, content) {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
});
}
else {
response.writeHead(500);
response.end('Something unexpectedly bad happened: '+error.code+' ..\n');
response.end();
}
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
});
server.listen(7341);
console.log('Teapot serving tea at: http://localhost:7341');