This repository has been archived by the owner on Oct 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-server.js
69 lines (62 loc) · 1.46 KB
/
web-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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var host = '0.0.0.0';
var port = 8080;
var fs = require('fs');
var path = require('path');
var http = require('http');
var mimeTypes = {
'': 'text/plain',
'.txt': 'text/plain',
'.html': 'text/html',
'.manifest': 'text/cache-manifest',
'.css': 'text/css',
'.js': 'application/javascript',
'.woff': 'application/font-woff',
'.woff2': 'application/font-woff2',
'.jpg': 'image/jpg',
'.png': 'image/png',
'.ico': 'image/x-icon',
'.mp4': 'video/mp4',
'.ogg': 'video/ogg',
'.webm': 'video/webm',
'.vtt': 'text/vtt'
};
function getFile(path, rs, mimeType) {
fs.readFile(path, function(err, contents) {
if (err) {
rs.writeHead(500);
rs.end();
} else {
rs.writeHead(200, {
'Content-Type': mimeType,
'Content-Length': contents.length
});
rs.end(contents);
}
});
}
http.createServer(function(rq,rs) {
var filename = rq.url;
if (rq.url === '/') {
filename = '/index.html';
}
var ext = path.extname(filename);
var localPath = __dirname + filename;
var mimeType = mimeTypes[ext];
if (mimeType) {
fs.exists(localPath, function(exists){
if (exists) {
console.log('Serving file: ' + localPath );
getFile(localPath, rs, mimeType || ext);
} else {
console.log('File not found: ' + localPath);
rs.writeHead(404);
rs.end();
}
});
} else {
console.log('Unknown file extensions: ' + ext);
rs.writeHead(404);
rs.end();
}
}).listen(8080, '0.0.0.0');
console.log('Server running at http://' + host + ':' + port + '/');