-
Notifications
You must be signed in to change notification settings - Fork 10
/
helpers.js
40 lines (35 loc) · 1.34 KB
/
helpers.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
module.exports = function() {
/**
* Response helper function
* @param {*} res
* @param {*} status
* @param {*} statusText
*/
response = function(res, status = 200, statusText = 'OK', headers = {}) {
if (typeof res === 'object') {
var { status, statusText, headers } = res;
res = res.res;
}
let newHeaders = new Headers(headers);
newHeaders.set('X-CloudFlare-Worker', 'Served by CloudFlare Worker.');
newHeaders.set('X-CloudFlare-Worker-Last-Modified', LAST_MODIFIED);
// let text = (async () => {
// return await res.text();
// })();
// Modify it.
//let modified = text + '<br/>More info here: <a href="https://github.com/anderly/cloudflare-worker-routing">https://github.com/anderly/cloudflare-worker-routing</a>';
var response = new Response(res, { status: status, statusText: statusText, headers: newHeaders });
return response;
}; //end response
/**
* Redirect helper function
* @param {*} uri
* @param {*} status
* @param {*} extraHeaders
*/
redirect = function(uri, status = 302, extraHeaders = {}) {
let headers = new Headers(extraHeaders);
headers.set('Location', uri);
return response('', status, 'Found', headers);
}; //end redirect
};