-
Notifications
You must be signed in to change notification settings - Fork 126
/
web.js
57 lines (48 loc) · 1.39 KB
/
web.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 http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
var server = http.createServer(function (req, res) {
console.log(req.method + ' ' + req.url);
var parsedUrl = url.parse(req.url);
var pathname = '.' + parsedUrl.pathname;
var mimeType = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'applicaion/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.doc': 'application/msword',
'.eot': 'application/vnd.ms-fontobject',
'.ttf': 'application/font-sfnt'
};
fs.exists(pathname, function (exist) {
if (!exist) {
res.statusCode = 404;
res.end('File ' + pathname + ' not found!');
return;
}
if (fs.statSync(pathname).isDirectory()) {
pathname += '/index.html';
}
fs.readFile(pathname, function (err, data) {
if (err) {
res.statusCode = 500;
res.end('Error getting the file: ' + err);
} else {
var ext = path.parse(pathname).ext;
res.setHeader('Content-type', mimeType[ext] || 'text/plain');
res.end(data);
}
});
});
});
var port = process.env.PORT || 3000;
server.listen(port);
console.log("Server listening on port " + port);