forked from idoco/ghost-test-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
69 lines (55 loc) · 2.33 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var path = require('path');
var ghost = require('ghost');
var express = require('express');
var Loadmill = require('express-loadmill');
var parentApp = express();
ghost({
config: path.join(__dirname, 'config.js')
}).then(function (ghostServer) {
parentApp.use(Loadmill({
verifyToken: "not-used",
// monitor: {
// // Required:
// apiToken: process.env.LOADMILL_API_TOKEN,
// // Default is TRUE:
// enabled: process.env.ENABLE_LOADMILL_MONITORING
// }
}));
/* The old way, before express-loadmill middleware
// Allow CORS
parentApp.use(function (req, res, next) {
var origin = req.header("Origin");
var requestMethod = req.header("Access-Control-Request-Method");
// we allow CORS from any origin
res.header("Access-Control-Allow-Origin", origin);
if (req.method === 'OPTIONS' && origin && requestMethod) {
// It's a pre-flight request:
var requestHeaders = req.header("Access-Control-Request-Headers");
setPreFlightHeaders(res, requestMethod || "", requestHeaders || "");
return res.sendStatus(204);
} else {
var exposedHeaders = req.header("Loadmill-Request-Expose-Headers") || "";
res.header("Access-Control-Expose-Headers", exposedHeaders);
}
return next();
});
function setPreFlightHeaders(res, allowedMethod, allowedHeaders) {
res.header({
// This header asks the browser not to pre-flight
// the same request URL again for the next 24 hours:
"Access-Control-Max-Age": "86400",
"Access-Control-Allow-Methods": allowedMethod,
"Access-Control-Allow-Headers": allowedHeaders
});
}
*/
// for automatic domain verification we always echo the challenge file name
parentApp.use("/loadmill-challenge/:fileName",function (req, res) {
const fileName = req.params.fileName;
res.send(fileName.substr(0, fileName.length - 4));
});
parentApp.use('/worker.js', express.static(path.join(__dirname, 'static/sw-bundle.js')))
parentApp.use('/inject.js', express.static(path.join(__dirname, './inject.js')))
parentApp.use(ghostServer.config.paths.subdir, ghostServer.rootApp);
ghostServer.start(parentApp);
});