-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (51 loc) · 1.37 KB
/
app.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
50
51
52
53
54
55
56
57
var MINE_TYPES = {
'html': 'text/html',
'txt': 'text/plain',
'css': 'text/css',
'js': 'text/javascript',
'json': 'application/json',
'png': 'image/png',
'jpg': 'image/jpeg'
};
var PORT = 3001;
var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
var root = process.cwd();
var server = http.createServer(function(request, response) {
var pathname = url.parse(request.url).pathname;
var realPath = path.join(root, pathname);
var ext = path.extname(realPath);
if (!ext) {
realPath = path.join(realPath, '/index.html');
ext = '.html'
}
fs.exists(realPath, function(exists) {
if (exists) {
fs.readFile(realPath, 'binary', function(err, file) {
if (!err) {
response.writeHead(200, {
'Content-Type': MINE_TYPES[ext.slice(1)] || 'text/plain'
});
response.write(file, 'binary');
response.end();
} else {
response.writeHead(500, {
'Content-Type': 'text/plain'
});
response.write('ERROR, the reason of error is ' + err.code + '; Error number is ' + err.errno + '.');
response.end();
}
})
} else {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.write('This request URL ' + pathname + ' was not found on this server.');
response.end();
}
});
});
server.listen(PORT);
console.log("server running at port " + PORT);