MCPcopy Index your code
hub / github.com/codecombat/codecombat / setupExpressMiddleware

Function setupExpressMiddleware

server_setup.js:64–111  ·  view source on GitHub ↗
(app)

Source from the content-addressed store, hash-verified

62};
63
64const setupExpressMiddleware = function(app) {
65 if (config.isProduction) {
66 morgan.format('prod', productionLogging);
67 app.use(morgan('prod'));
68 app.use(compression({filter(req, res) {
69 if (req.headers.host === 'codecombat.com') { return false; } // CloudFlare will gzip it for us on codecombat.com
70 return compressible(res.getHeader('Content-Type'));
71 }
72 })
73 );
74 } else if (!global.testing || config.TRACE_ROUTES) {
75 morgan.format('dev', developmentLogging);
76 app.use(morgan('dev'));
77 }
78
79 app.use(function(req, res, next) {
80 res.header('X-Cluster-ID', config.clusterID);
81 return next();
82 });
83
84 app.use('/', express.static(path.join(publicPath, 'templates', 'static')));
85
86 if ((config.buildInfo.sha !== 'dev') && config.isProduction) {
87 app.use(`/${config.buildInfo.sha}`, express.static(publicPath, {maxAge: '1y'}));
88 } else {
89 app.use('/dev', express.static(publicPath, {maxAge: 0})); // CloudFlare overrides maxAge, and we don't want local development caching.
90 }
91
92 app.use(express.static(publicPath, {maxAge: 0}));
93
94 setupProxyMiddleware(app); // TODO: Flatten setup into one function. This doesn't fit its function name.
95
96 try {
97 app.use(require('serve-favicon')(path.join(publicPath, 'images', 'favicon', `favicon-${productSuffix}`, 'favicon.ico')));
98 } catch (e) {
99 console.error(`Error. Couldn't find ${path.join(publicPath, 'images', 'favicon', 'favicon-' + productSuffix)}. It is likely that the ${publicFolderName} folder is not built. Try:\n\n npm run build\n\nfor an initial build, or\n\n npm run dev\n\nfor live rebuilding of your front-end changes. If those don't work, make sure you are running the correct version of node and have installed all dependencies with:\n\n npm install --also=dev\n`);
100 process.exit(1);
101 }
102 app.use(require('cookie-parser')());
103 app.use(require('body-parser').json({limit: '25mb', strict: false}));
104 app.use(require('body-parser').urlencoded({extended: true, limit: '25mb'}));
105 app.use(require('method-override')());
106 return app.use(require('cookie-session')({
107 key: 'codecombat.sess',
108 secret: config.cookie_secret
109 })
110 );
111};
112
113const setupCountryRedirectMiddleware = function(app, country, host) {
114 if (country == null) { country = 'china'; }

Callers 1

server_setup.jsFile · 0.85

Calls 2

setupProxyMiddlewareFunction · 0.85
errorMethod · 0.45

Tested by

no test coverage detected