Reputation: 3415
In my node.js app (with Express), I have a final else condition to catch unknown paths and respond with a file named '404.html':
else if(req.url !== '/json' && req.url !== '/version' && req.url !== "/json/version"){
res.status(404).send('<script>location.href = "/404.html";</script>');
}
This is working fine if for example someone is trying to access a invalid path www.myApp.com/aaldkfjd
or www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj
, which are invalid so it will go in the else and sends the '404.html' file.
but when inspecting the server response in the client, I noticed it's a 304, instead of a 404.
<!doctype html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html">
<title>My Awesome 404 page</title>
<link rel="stylesheet" type="text/css" media="all" href="/css/styles.css"/>
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico">
</head>
<body style="text-align:center;">
<div id="loginDiv">
<h1 id="loginHeader">404...We don't know that route!</h1>
</div>
<img src="images/myImageMedium.png" alt="404 image">
</body>
</html>
Upvotes: 2
Views: 321
Reputation: 1
you could event try the following code below.
else if(req.url !== '/json' && req.url !== '/version' && req.url !== "/json/version"){
res.write('<script>location.href = "/404.html";</script>'); res.status(404).end();
}
Upvotes: -3
Reputation: 664
Your server is sending a script that redirects to the 404 page, thus the 304 status.
The best practice here would be to have node send the "My Awesome 404 page" itself.
So:
res.status(404).sendFile('public/404.html')
Upvotes: 2
Reputation: 943108
The browser gets a 404 status and loads the 404 error page.
The error page contains JavaScript which causes the browser to leave the 404 page and navigate to a new page.
That page is loaded with a 304 status.
You should serve your actual 404 message when you output a 404 error and not redirect the client.
Upvotes: 3