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

Function setupProxyMiddleware

server_setup.js:308–358  ·  view source on GitHub ↗
(app)

Source from the content-addressed store, hash-verified

306};
307
308var setupProxyMiddleware = function(app) {
309 if (config.isProduction) { return; }
310 if (!config.proxy) { return; }
311
312 // Don't proxy static files with sha prefixes, redirect them
313 const regex = /\/[0-9a-f]{40}\/.*/;
314 const regex2 = /\/[0-9a-f]{40}-[0-9a-f]{40}\/.*/;
315 // based on new format of branch name + date
316 const regex3 = /^\/(production|next)-\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}\/.*/;
317 app.use(function(req, res, next) {
318 let newPath;
319 if (regex.test(req.path)) {
320 newPath = req.path.slice(41);
321 return res.redirect(newPath);
322 }
323 if (regex2.test(req.path)) {
324 newPath = req.path.slice(82);
325 return res.redirect(newPath);
326 }
327 if (regex3.test(req.path)) {
328 const split = req.path.split('/');
329 newPath = '/' + split.slice(2).join('/');
330 return res.redirect(newPath);
331 }
332 return next();
333 });
334
335 const httpProxy = require('http-proxy');
336
337 let target = process.env.COCO_PROXY_TARGET || `https://direct.staging.${config.product}.com`;
338 const headers = {};
339
340 if (process.env.COCO_PROXY_NEXT) {
341 target = `https://direct.next.${config.product}.com`;
342 headers['Host'] = `next.${config.product}.com`;
343 }
344
345 const proxy = httpProxy.createProxyServer({
346 target,
347 headers,
348 secure: false
349 });
350 console.info('Using dev proxy server');
351 return app.use(function(req, res, next) {
352 req.proxied = true;
353 return proxy.web(req, res, function(e) {
354 console.warn("Failed to proxy: ", e);
355 return res.status(502).send({message: 'Proxy failed'});
356 });
357 });
358};

Callers 1

setupExpressMiddlewareFunction · 0.85

Calls 1

statusMethod · 0.80

Tested by

no test coverage detected