Skip to content

Commit

Permalink
Send status code to server response for redirects (#408)
Browse files Browse the repository at this point in the history
Also simplified the logic a bit to reduce duplication

Closes #404
  • Loading branch information
JoelMarcey committed Jan 23, 2018
1 parent 036b84d commit c81609d
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions lib/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c81609d

Please sign in to comment.