In Node.js setting status to 404 reponds with a 304 instead









up vote
1
down vote

favorite
1












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.



enter image description here






<!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>












share|improve this question























  • 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















up vote
1
down vote

favorite
1












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.



enter image description here






<!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>












share|improve this question























  • 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













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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.



enter image description here






<!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>












share|improve this question















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.



enter image description here






<!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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















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













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')





share|improve this answer




















  • 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


















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.






share|improve this answer



























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






    share|improve this answer




















    • Implemented and this still comes back with a 304 status.
      – ConfusedDeer
      Nov 9 at 16:21










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



    );













    draft saved

    draft discarded


















    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')





    share|improve this answer




















    • 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















    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')





    share|improve this answer




















    • 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













    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')





    share|improve this answer












    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')






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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

















    • 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













    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.






    share|improve this answer
























      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.






      share|improve this answer






















        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 15:52









        Quentin

        637k718601030




        637k718601030




















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






            share|improve this answer




















            • Implemented and this still comes back with a 304 status.
              – ConfusedDeer
              Nov 9 at 16:21














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






            share|improve this answer




















            • Implemented and this still comes back with a 304 status.
              – ConfusedDeer
              Nov 9 at 16:21












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






            share|improve this answer












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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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
















            • 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

















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            How do I collapse sections of code in Visual Studio Code for Windows?

            ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ