2021-06-15 19:01:22 +03:00
|
|
|
const debug = require('@tryghost/debug')('shared:express');
|
2020-04-25 22:53:58 +03:00
|
|
|
const express = require('express');
|
2021-10-11 13:06:03 +03:00
|
|
|
const {createLazyRouter} = require('express-lazy-router');
|
2020-04-25 22:53:58 +03:00
|
|
|
const sentry = require('./sentry');
|
|
|
|
|
2021-10-11 13:06:03 +03:00
|
|
|
const lazyLoad = createLazyRouter();
|
|
|
|
|
2020-05-01 21:29:42 +03:00
|
|
|
module.exports = (name) => {
|
|
|
|
debug('new app start', name);
|
2020-04-25 22:53:58 +03:00
|
|
|
const app = express();
|
2020-05-01 21:29:42 +03:00
|
|
|
app.set('name', name);
|
2020-04-25 22:53:58 +03:00
|
|
|
|
|
|
|
// Make sure 'req.secure' is valid for proxied requests
|
|
|
|
// (X-Forwarded-Proto header will be checked, if present)
|
|
|
|
app.enable('trust proxy');
|
|
|
|
|
|
|
|
// Sentry must be our first error handler. Mounting it here means all custom error handlers will come after
|
|
|
|
app.use(sentry.errorHandler);
|
|
|
|
|
2021-10-11 13:06:03 +03:00
|
|
|
app.lazyUse = function lazyUse(mountPath, requireFn) {
|
|
|
|
app.use(mountPath, lazyLoad(() => {
|
|
|
|
debug(`lazy-loading on ${mountPath}`);
|
|
|
|
return Promise.resolve(requireFn());
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2020-05-01 21:29:42 +03:00
|
|
|
debug('new app end', name);
|
2020-04-25 22:53:58 +03:00
|
|
|
return app;
|
|
|
|
};
|
2020-05-01 21:29:42 +03:00
|
|
|
|
|
|
|
// Wrap the main express router call
|
|
|
|
// This is mostly an experiement, and can likely be removed soon
|
|
|
|
module.exports.Router = (name, options) => {
|
|
|
|
debug('new Router start', name);
|
|
|
|
const router = express.Router(options);
|
|
|
|
|
|
|
|
router.use(sentry.errorHandler);
|
|
|
|
|
|
|
|
debug('new Router end', name);
|
|
|
|
return router;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.static = express.static;
|
|
|
|
|
|
|
|
// Export the OG module for testing based on the internals
|
|
|
|
module.exports._express = express;
|