-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.js
38 lines (34 loc) · 1.01 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
var fs = require('fs');
var argo = require('argo');
var mime = require('mime');
argo()
.use(function(handle) {
handle('response', function(env, next) {
if (env.response.statusCode === 404) {
env.response.body = 'Not Found';
}
next(env);
});
})
.get('.+', function(handle) {
handle('request', function(env, next) {
if (env.request.url === '/' + __filename.split('/').pop() ||
!!~env.request.url.indexOf('./')) {
env.response.statusCode = 404;
return next(env);
}
var filename = __dirname +
(env.request.url === '/' ? '/index.html' : env.request.url)
fs.stat(filename, function(err, stat) {
if (err) {
env.response.statusCode = 404;
return next(env);
}
var stream = fs.createReadStream(filename);
env.response.setHeader('Content-Type', mime.lookup(filename));
env.response.body = stream;
next(env);
});
});
})
.listen(process.env.PORT || 3001);