-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
40 lines (32 loc) · 996 Bytes
/
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
var baseUrl = 'http://localhost:8080';
var childProcess = require('child_process');
var fs = require('fs');
var server = require('http').createServer(handler);
var mime = {
'css' : 'text/css',
'html' : 'text/html',
'png' : 'image/png',
'js' : 'application/javascript',
'woff' : 'application/x-font-woff'
};
function handler(req, res) {
var url = (req.url.split('?')[0]).substr(1);
if (url == '') url = 'index.html';
var exploded = url.split('.');
var contentType = mime[exploded[exploded.length - 1]] || 'application/octet-stream';
res.setHeader('Content-Type', contentType);
console.log('GET', url);
fs.readFile(
url,
function (err, data) {
if (err) {
res.writeHead(404);
return res.end('Error loading ' + url);
}
res.writeHead(200);
res.end(data);
}
);
}
server.listen(8080);
childProcess.exec('start chrome ' + baseUrl);