-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
dev-server.js
54 lines (45 loc) · 1.51 KB
/
dev-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
/**
* dev-server - serves static resources for developing "earth" locally
*/
"use strict";
console.log("============================================================");
console.log(new Date().toISOString() + " - Starting");
var util = require("util");
/**
* Returns true if the response should be compressed.
*/
function compressionFilter(req, res) {
return (/json|text|javascript|font/).test(res.getHeader('Content-Type'));
}
/**
* Adds headers to a response to enable caching.
*/
function cacheControl() {
return function(req, res, next) {
res.setHeader("Cache-Control", "public, max-age=300");
return next();
};
}
function logger() {
express.logger.token("date", function() {
return new Date().toISOString();
});
express.logger.token("response-all", function(req, res) {
return (res._header ? res._header : "").trim();
});
express.logger.token("request-all", function(req, res) {
return util.inspect(req.headers);
});
return express.logger(
':date - info: :remote-addr :req[cf-connecting-ip] :req[cf-ipcountry] :method :url HTTP/:http-version ' +
'":user-agent" :referrer :req[cf-ray] :req[accept-encoding]\\n:request-all\\n\\n:response-all\\n');
}
var port = process.argv[2];
var express = require("express");
var app = express();
app.use(cacheControl());
app.use(express.compress({filter: compressionFilter}));
app.use(logger());
app.use(express.static("public"));
app.listen(port);
console.log("Listening on port " + port + "...");