In Node.js setting status to 404 reponds with a 304 instead
up vote
1
down vote
favorite
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>javascript node.js express
add a comment |
up vote
1
down vote
favorite
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>javascript node.js express
I suspect that if you tell your browser's devtools not to clear the network tab when going from one page to the next, you'll find that it's a 404 (for the invalid URL) followed by a 304 (for the404.htmlresource).
– T.J. Crowder
Nov 9 at 15:52
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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>javascript node.js express
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><!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><!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>javascript node.js express
javascript node.js express
edited Nov 9 at 15:53
asked Nov 9 at 15:49
ConfusedDeer
1,59042447
1,59042447
I suspect that if you tell your browser's devtools not to clear the network tab when going from one page to the next, you'll find that it's a 404 (for the invalid URL) followed by a 304 (for the404.htmlresource).
– T.J. Crowder
Nov 9 at 15:52
add a comment |
I suspect that if you tell your browser's devtools not to clear the network tab when going from one page to the next, you'll find that it's a 404 (for the invalid URL) followed by a 304 (for the404.htmlresource).
– T.J. Crowder
Nov 9 at 15:52
I suspect that if you tell your browser's devtools not to clear the network tab when going from one page to the next, you'll find that it's a 404 (for the invalid URL) followed by a 304 (for the
404.html resource).– T.J. Crowder
Nov 9 at 15:52
I suspect that if you tell your browser's devtools not to clear the network tab when going from one page to the next, you'll find that it's a 404 (for the invalid URL) followed by a 304 (for the
404.html resource).– T.J. Crowder
Nov 9 at 15:52
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
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')
I implemented your suggestion and it returns a 404 with the file displayed (works): res.status(404).sendFile(path.join(__dirname, '..','/public/404.html')); BUT oddly enough it only works with let's say 'www.myApp.com/aaldkfjd/' but NOT 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' and by that I mean it sends the file, but the image is not show when ther is an extra '/' so 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' doesn't show the image.
– ConfusedDeer
Nov 9 at 16:13
1
in your html, use: /images/... it is looking for the image in the subdirectory /aaldkfjd/
– bluejack
Nov 9 at 16:21
1
That worked. I'm now getting the a proper 404 response while still being redirected to the custom 404 page. Probably marking your answer as accepted.
– ConfusedDeer
Nov 9 at 16:38
add a comment |
up vote
2
down vote
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.
add a comment |
up vote
-3
down vote
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();
Implemented and this still comes back with a 304 status.
– ConfusedDeer
Nov 9 at 16:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229019%2fin-node-js-setting-status-to-404-reponds-with-a-304-instead%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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')
I implemented your suggestion and it returns a 404 with the file displayed (works): res.status(404).sendFile(path.join(__dirname, '..','/public/404.html')); BUT oddly enough it only works with let's say 'www.myApp.com/aaldkfjd/' but NOT 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' and by that I mean it sends the file, but the image is not show when ther is an extra '/' so 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' doesn't show the image.
– ConfusedDeer
Nov 9 at 16:13
1
in your html, use: /images/... it is looking for the image in the subdirectory /aaldkfjd/
– bluejack
Nov 9 at 16:21
1
That worked. I'm now getting the a proper 404 response while still being redirected to the custom 404 page. Probably marking your answer as accepted.
– ConfusedDeer
Nov 9 at 16:38
add a comment |
up vote
1
down vote
accepted
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')
I implemented your suggestion and it returns a 404 with the file displayed (works): res.status(404).sendFile(path.join(__dirname, '..','/public/404.html')); BUT oddly enough it only works with let's say 'www.myApp.com/aaldkfjd/' but NOT 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' and by that I mean it sends the file, but the image is not show when ther is an extra '/' so 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' doesn't show the image.
– ConfusedDeer
Nov 9 at 16:13
1
in your html, use: /images/... it is looking for the image in the subdirectory /aaldkfjd/
– bluejack
Nov 9 at 16:21
1
That worked. I'm now getting the a proper 404 response while still being redirected to the custom 404 page. Probably marking your answer as accepted.
– ConfusedDeer
Nov 9 at 16:38
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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')
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')
answered Nov 9 at 16:00
bluejack
198112
198112
I implemented your suggestion and it returns a 404 with the file displayed (works): res.status(404).sendFile(path.join(__dirname, '..','/public/404.html')); BUT oddly enough it only works with let's say 'www.myApp.com/aaldkfjd/' but NOT 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' and by that I mean it sends the file, but the image is not show when ther is an extra '/' so 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' doesn't show the image.
– ConfusedDeer
Nov 9 at 16:13
1
in your html, use: /images/... it is looking for the image in the subdirectory /aaldkfjd/
– bluejack
Nov 9 at 16:21
1
That worked. I'm now getting the a proper 404 response while still being redirected to the custom 404 page. Probably marking your answer as accepted.
– ConfusedDeer
Nov 9 at 16:38
add a comment |
I implemented your suggestion and it returns a 404 with the file displayed (works): res.status(404).sendFile(path.join(__dirname, '..','/public/404.html')); BUT oddly enough it only works with let's say 'www.myApp.com/aaldkfjd/' but NOT 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' and by that I mean it sends the file, but the image is not show when ther is an extra '/' so 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' doesn't show the image.
– ConfusedDeer
Nov 9 at 16:13
1
in your html, use: /images/... it is looking for the image in the subdirectory /aaldkfjd/
– bluejack
Nov 9 at 16:21
1
That worked. I'm now getting the a proper 404 response while still being redirected to the custom 404 page. Probably marking your answer as accepted.
– ConfusedDeer
Nov 9 at 16:38
I implemented your suggestion and it returns a 404 with the file displayed (works): res.status(404).sendFile(path.join(__dirname, '..','/public/404.html')); BUT oddly enough it only works with let's say 'www.myApp.com/aaldkfjd/' but NOT 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' and by that I mean it sends the file, but the image is not show when ther is an extra '/' so 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' doesn't show the image.
– ConfusedDeer
Nov 9 at 16:13
I implemented your suggestion and it returns a 404 with the file displayed (works): res.status(404).sendFile(path.join(__dirname, '..','/public/404.html')); BUT oddly enough it only works with let's say 'www.myApp.com/aaldkfjd/' but NOT 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' and by that I mean it sends the file, but the image is not show when ther is an extra '/' so 'www.myApp.com/aaldkfjd/jkjadklfja/dkjfakj' doesn't show the image.
– ConfusedDeer
Nov 9 at 16:13
1
1
in your html, use: /images/... it is looking for the image in the subdirectory /aaldkfjd/
– bluejack
Nov 9 at 16:21
in your html, use: /images/... it is looking for the image in the subdirectory /aaldkfjd/
– bluejack
Nov 9 at 16:21
1
1
That worked. I'm now getting the a proper 404 response while still being redirected to the custom 404 page. Probably marking your answer as accepted.
– ConfusedDeer
Nov 9 at 16:38
That worked. I'm now getting the a proper 404 response while still being redirected to the custom 404 page. Probably marking your answer as accepted.
– ConfusedDeer
Nov 9 at 16:38
add a comment |
up vote
2
down vote
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.
add a comment |
up vote
2
down vote
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.
add a comment |
up vote
2
down vote
up vote
2
down vote
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.
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.
answered Nov 9 at 15:52
Quentin
637k718601030
637k718601030
add a comment |
add a comment |
up vote
-3
down vote
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();
Implemented and this still comes back with a 304 status.
– ConfusedDeer
Nov 9 at 16:21
add a comment |
up vote
-3
down vote
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();
Implemented and this still comes back with a 304 status.
– ConfusedDeer
Nov 9 at 16:21
add a comment |
up vote
-3
down vote
up vote
-3
down vote
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();
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();
answered Nov 9 at 16:16
codecode
1
1
Implemented and this still comes back with a 304 status.
– ConfusedDeer
Nov 9 at 16:21
add a comment |
Implemented and this still comes back with a 304 status.
– ConfusedDeer
Nov 9 at 16:21
Implemented and this still comes back with a 304 status.
– ConfusedDeer
Nov 9 at 16:21
Implemented and this still comes back with a 304 status.
– ConfusedDeer
Nov 9 at 16:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229019%2fin-node-js-setting-status-to-404-reponds-with-a-304-instead%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I suspect that if you tell your browser's devtools not to clear the network tab when going from one page to the next, you'll find that it's a 404 (for the invalid URL) followed by a 304 (for the
404.htmlresource).– T.J. Crowder
Nov 9 at 15:52