Skip to content

Commit f5c4a44

Browse files
author
Robert Jackson
committed
Ensure 404's receive the proper response code.
Prior to this change, requesting a URL that did not exist in the app resulted in a 500 response code (which is not correct). When we encounter an unrecognized URL we should simply call `next` and allow any downstream middlewares to handle the request. If nothing handles the request, Express will set the status code to 404 on its own.
1 parent f23226d commit f5c4a44

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ function fastbootExpressMiddleware(distPath, options) {
5858
}
5959

6060
function failure(error) {
61-
if (error.name !== "UnrecognizedURLError") {
61+
if (error.name === "UnrecognizedURLError") {
62+
next();
63+
} else {
6264
res.status(500);
65+
next(error);
6366
}
64-
next(error);
6567
}
6668
};
6769
}

test/middleware-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ describe("FastBoot", function() {
5252
});
5353
});
5454

55+
it("returns 404 when navigating to a URL that doesn't exist", function() {
56+
let middleware = fastbootMiddleware(fixture('basic-app'));
57+
server = new TestHTTPServer(middleware);
58+
59+
return server.start()
60+
.then(() => server.request('/foo-bar-baz/non-existent'))
61+
.catch((result) => {
62+
expect(result.statusCode).to.equal(404);
63+
});
64+
});
65+
5566
it("returns a 500 error if an error occurs", function() {
5667
let middleware = fastbootMiddleware({
5768
distPath: fixture('rejected-promise')

0 commit comments

Comments
 (0)