-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
52 lines (47 loc) · 1.09 KB
/
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
var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url');
var config = require('./config.json');
var server = http.createServer(function(req, res){
var dir = path.join(__dirname, 'www')
,uri = url.parse(req.url).pathname
,filename = path.join(dir, unescape(uri))
,indexFilename = path.join(dir, unescape('index.html'))
,stats;
try
{
stats = fs.lstatSync(filename);
}
catch (e)
{
stats = false;
}
if (stats && stats.isFile())
{
// path exists, is a file
var mimeType = config.server.mimetypes[path.extname(filename)
.split(".")[1]];
res.writeHead(200,
{
'Content-Type': mimeType
});
var fileStream =
fs.createReadStream(filename)
.pipe(res);
}
else if (stats && stats.isDirectory())
{
// path exists, is a directory
res.writeHead(200,
{
'Content-Type': "text/html"
});
var fileStream =
fs.createReadStream(indexFilename)
.pipe(res);
}
}).listen(config.server.port, function() {
console.log('\tServer running at: http://localhost:'+config.server.port+' ...');
});
module.exports = server;