-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.js
53 lines (45 loc) · 1.51 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
53
var fs = require('fs');
var path = require('path');
var argo = require('argo');
var mime = require('mime');
var startTime = new Date( parseInt(new Date().getTime() / 1000)*1000 );
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 + '/dist' +
(env.request.url === '/' ? '/index.html' : env.request.url)
if(env.request.headers['if-modified-since']) {
var d = new Date(env.request.headers['if-modified-since']);
if (startTime.getTime() <= d.getTime()) {
env.response.statusCode = 304;
return next(env);
}
}
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.setHeader('Cache-Control', 'public, max-age=31536000');
env.response.setHeader('Last-Modified', startTime);
env.response.body = stream;
next(env);
});
});
})
.listen(process.env.PORT || 3001);