-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
62 lines (48 loc) · 1.4 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var _ = require('lodash');
var prerender = require('prerender-node');
var send = require('response-send');
var url = require('url');
module.exports = function (options) {
options = options || {};
prerender.set('prerenderServiceUrl', options.host);
prerender.set('prerenderToken', options.token);
var render = function (req, res, next) {
decorateRequestObject(req);
decorateResponseObject(req, res);
prerender.whitelisted(req.service.config.whitelist);
prerender.blacklisted(req.service.config.blacklist);
prerender(req, res, next);
};
render.matches = function (req) {
return prerender.shouldShowPrerenderedPage(req);
};
// keep it backwards compatible
render.matchesRequest = render.matches;
return render;
};
function decorateRequestObject (req) {
req.protocol = (req.connection.encrypted) ? 'https' : 'http';
req.get = function (name) {
return _(req.headers)
.map(function (value, header) {
return [header.toLowerCase(), value];
})
.zipObject()
.find(function (value, header) {
return header === name.toLowerCase();
});
};
}
function decorateResponseObject (req, res) {
res.req = req;
res.send = send;
res.status = function (code) {
res.statusCode = code;
return res;
};
res.set = function (headers) {
_(headers).each(function (value, name) {
res.setHeader(name, value);
});
};
}