From c81609d393e9e1fe2b63b69028b0624091a3e440 Mon Sep 17 00:00:00 2001 From: Joel Marcey Date: Tue, 23 Jan 2018 13:13:25 -0800 Subject: [PATCH] Send status code to server response for redirects (#408) Also simplified the logic a bit to reduce duplication Closes #404 --- lib/server/server.js | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/lib/server/server.js b/lib/server/server.js index 5a81193aeb3a..eae2b23f28ed 100644 --- a/lib/server/server.js +++ b/lib/server/server.js @@ -514,28 +514,25 @@ function execute(port) { app.use(siteConfig.baseUrl, express.static(CWD + '/static')); app.use(siteConfig.baseUrl, express.static(__dirname + '/../static')); - // "redirect" requests to pages ending with "/" or no extension so that - // request to "...blog" returns same result as "...blog/index.html" + // "redirect" requests to pages ending with "/" or no extension so that, + // for example, request to "blog" returns same result as "blog/index.html" app.get(/\/[^\.]*\/?$/, (req, res) => { - if (req.path.toString().endsWith('/')) { - request.get( - 'http://localhost:' + port + req.path + 'index.html', - (err, response, body) => { - if (!err) { - res.send(body); + let slash = req.path.toString().endsWith('/') ? '' : '/'; + request.get( + 'http://localhost:' + port + req.path + slash + 'index.html', + (err, response, body) => { + console.log(response.statusCode); + if (!err) { + if (response) { + res.status(response.statusCode).send(body); + } else { + console.error('No response'); } + } else { + console.error('request failed:', err); } - ); - } else { - request.get( - 'http://localhost:' + port + req.path + '/index.html', - (err, response, body) => { - if (!err) { - res.send(body); - } - } - ); - } + } + ); }); app.listen(port);