Send status code to server response for redirects (#408)

Also simplified the logic a bit to reduce duplication

Closes #404
This commit is contained in:
Joel Marcey 2018-01-23 13:13:25 -08:00 committed by GitHub
parent 036b84df90
commit c81609d393
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);