wildcard * route responding to all requests











up vote
0
down vote

favorite












I have a working Node.js app built with the Express.js framework. All routes in the app are post routes ie.



app.post('/login', function(req, res){
//do login validation
});

app.post('/AppHomepage', function(req, res){
//send user app dashboard
});
etc.


An issue I was having was, if the user had entered an undefined route eg. appdomain.com/thisroutedoesnotexist, they would be returned 'Cannot GET /thisroutedoesnotexist'



I therefore followed the advice in this answer: https://stackoverflow.com/a/25216843 and added



app.get('*', function(req, res) {
res.redirect('/');
});


as the very last route. This works perfectly fine for redirecting undefined requests to the index page and solved the issue.



However, I have found this wildcard redirect is also invoked when LEGITIMATE requests are made. ie. if the user requests /AppHomepage, both



app.post('/AppHomepage', function(req, res){
//send user app dashboard
});


AND



app.get('*', function(req, res) {
res.redirect('/');
});


catches and responds to the request. The user is indeed getting the correct res.render of the homepage, however a console.log inside the index route is also printing meaning that the wildcard route catching the /AppHomepage request even after the app.post('/AppHomepage') route has picked up and served the request.



Can anyone decipher as to why that would be happening?



EDIT:



I know now that even POST requests by the browser will be a GET and that is why the GET wildcard is picking up on the redirects.



My question now is there a way to redirect only undefined requests instead of all GET requests?










share|improve this question
























  • Request to /AppHomepage route indeed a GET not POST as your wildcard ( * ) route is for GET request only. Try loggin the request method in wildcard route itself.
    – front_end_dev
    Nov 11 at 4:43












  • Ok fair enough I see that now. Would you know of any other way to redirect only the undefined GET requests and not those that have an assigned app.post function?
    – R.Snow
    Nov 11 at 5:17










  • Put this in the end of file app._router.stack.forEach(function(middleware){ if(middleware.route){ console.log(middleware.route); } }); It will print all the register routes on the app based on that you can create your logic to either redirect or not.
    – front_end_dev
    Nov 11 at 5:33










  • Thanks for that. Your code successfully gives me a list of routes which I can compare incoming requests against. However the issue now is, I cannot correctly get the request path. None of the suggested req.originalUrl, req.url, req.baseUrl or req.path return the path on the browser request. I guess I'll have to ask a new question for that. But thanks anyway
    – R.Snow
    Nov 11 at 5:59

















up vote
0
down vote

favorite












I have a working Node.js app built with the Express.js framework. All routes in the app are post routes ie.



app.post('/login', function(req, res){
//do login validation
});

app.post('/AppHomepage', function(req, res){
//send user app dashboard
});
etc.


An issue I was having was, if the user had entered an undefined route eg. appdomain.com/thisroutedoesnotexist, they would be returned 'Cannot GET /thisroutedoesnotexist'



I therefore followed the advice in this answer: https://stackoverflow.com/a/25216843 and added



app.get('*', function(req, res) {
res.redirect('/');
});


as the very last route. This works perfectly fine for redirecting undefined requests to the index page and solved the issue.



However, I have found this wildcard redirect is also invoked when LEGITIMATE requests are made. ie. if the user requests /AppHomepage, both



app.post('/AppHomepage', function(req, res){
//send user app dashboard
});


AND



app.get('*', function(req, res) {
res.redirect('/');
});


catches and responds to the request. The user is indeed getting the correct res.render of the homepage, however a console.log inside the index route is also printing meaning that the wildcard route catching the /AppHomepage request even after the app.post('/AppHomepage') route has picked up and served the request.



Can anyone decipher as to why that would be happening?



EDIT:



I know now that even POST requests by the browser will be a GET and that is why the GET wildcard is picking up on the redirects.



My question now is there a way to redirect only undefined requests instead of all GET requests?










share|improve this question
























  • Request to /AppHomepage route indeed a GET not POST as your wildcard ( * ) route is for GET request only. Try loggin the request method in wildcard route itself.
    – front_end_dev
    Nov 11 at 4:43












  • Ok fair enough I see that now. Would you know of any other way to redirect only the undefined GET requests and not those that have an assigned app.post function?
    – R.Snow
    Nov 11 at 5:17










  • Put this in the end of file app._router.stack.forEach(function(middleware){ if(middleware.route){ console.log(middleware.route); } }); It will print all the register routes on the app based on that you can create your logic to either redirect or not.
    – front_end_dev
    Nov 11 at 5:33










  • Thanks for that. Your code successfully gives me a list of routes which I can compare incoming requests against. However the issue now is, I cannot correctly get the request path. None of the suggested req.originalUrl, req.url, req.baseUrl or req.path return the path on the browser request. I guess I'll have to ask a new question for that. But thanks anyway
    – R.Snow
    Nov 11 at 5:59















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a working Node.js app built with the Express.js framework. All routes in the app are post routes ie.



app.post('/login', function(req, res){
//do login validation
});

app.post('/AppHomepage', function(req, res){
//send user app dashboard
});
etc.


An issue I was having was, if the user had entered an undefined route eg. appdomain.com/thisroutedoesnotexist, they would be returned 'Cannot GET /thisroutedoesnotexist'



I therefore followed the advice in this answer: https://stackoverflow.com/a/25216843 and added



app.get('*', function(req, res) {
res.redirect('/');
});


as the very last route. This works perfectly fine for redirecting undefined requests to the index page and solved the issue.



However, I have found this wildcard redirect is also invoked when LEGITIMATE requests are made. ie. if the user requests /AppHomepage, both



app.post('/AppHomepage', function(req, res){
//send user app dashboard
});


AND



app.get('*', function(req, res) {
res.redirect('/');
});


catches and responds to the request. The user is indeed getting the correct res.render of the homepage, however a console.log inside the index route is also printing meaning that the wildcard route catching the /AppHomepage request even after the app.post('/AppHomepage') route has picked up and served the request.



Can anyone decipher as to why that would be happening?



EDIT:



I know now that even POST requests by the browser will be a GET and that is why the GET wildcard is picking up on the redirects.



My question now is there a way to redirect only undefined requests instead of all GET requests?










share|improve this question















I have a working Node.js app built with the Express.js framework. All routes in the app are post routes ie.



app.post('/login', function(req, res){
//do login validation
});

app.post('/AppHomepage', function(req, res){
//send user app dashboard
});
etc.


An issue I was having was, if the user had entered an undefined route eg. appdomain.com/thisroutedoesnotexist, they would be returned 'Cannot GET /thisroutedoesnotexist'



I therefore followed the advice in this answer: https://stackoverflow.com/a/25216843 and added



app.get('*', function(req, res) {
res.redirect('/');
});


as the very last route. This works perfectly fine for redirecting undefined requests to the index page and solved the issue.



However, I have found this wildcard redirect is also invoked when LEGITIMATE requests are made. ie. if the user requests /AppHomepage, both



app.post('/AppHomepage', function(req, res){
//send user app dashboard
});


AND



app.get('*', function(req, res) {
res.redirect('/');
});


catches and responds to the request. The user is indeed getting the correct res.render of the homepage, however a console.log inside the index route is also printing meaning that the wildcard route catching the /AppHomepage request even after the app.post('/AppHomepage') route has picked up and served the request.



Can anyone decipher as to why that would be happening?



EDIT:



I know now that even POST requests by the browser will be a GET and that is why the GET wildcard is picking up on the redirects.



My question now is there a way to redirect only undefined requests instead of all GET requests?







javascript node.js express routes






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 5:22

























asked Nov 11 at 4:32









R.Snow

2115




2115












  • Request to /AppHomepage route indeed a GET not POST as your wildcard ( * ) route is for GET request only. Try loggin the request method in wildcard route itself.
    – front_end_dev
    Nov 11 at 4:43












  • Ok fair enough I see that now. Would you know of any other way to redirect only the undefined GET requests and not those that have an assigned app.post function?
    – R.Snow
    Nov 11 at 5:17










  • Put this in the end of file app._router.stack.forEach(function(middleware){ if(middleware.route){ console.log(middleware.route); } }); It will print all the register routes on the app based on that you can create your logic to either redirect or not.
    – front_end_dev
    Nov 11 at 5:33










  • Thanks for that. Your code successfully gives me a list of routes which I can compare incoming requests against. However the issue now is, I cannot correctly get the request path. None of the suggested req.originalUrl, req.url, req.baseUrl or req.path return the path on the browser request. I guess I'll have to ask a new question for that. But thanks anyway
    – R.Snow
    Nov 11 at 5:59




















  • Request to /AppHomepage route indeed a GET not POST as your wildcard ( * ) route is for GET request only. Try loggin the request method in wildcard route itself.
    – front_end_dev
    Nov 11 at 4:43












  • Ok fair enough I see that now. Would you know of any other way to redirect only the undefined GET requests and not those that have an assigned app.post function?
    – R.Snow
    Nov 11 at 5:17










  • Put this in the end of file app._router.stack.forEach(function(middleware){ if(middleware.route){ console.log(middleware.route); } }); It will print all the register routes on the app based on that you can create your logic to either redirect or not.
    – front_end_dev
    Nov 11 at 5:33










  • Thanks for that. Your code successfully gives me a list of routes which I can compare incoming requests against. However the issue now is, I cannot correctly get the request path. None of the suggested req.originalUrl, req.url, req.baseUrl or req.path return the path on the browser request. I guess I'll have to ask a new question for that. But thanks anyway
    – R.Snow
    Nov 11 at 5:59


















Request to /AppHomepage route indeed a GET not POST as your wildcard ( * ) route is for GET request only. Try loggin the request method in wildcard route itself.
– front_end_dev
Nov 11 at 4:43






Request to /AppHomepage route indeed a GET not POST as your wildcard ( * ) route is for GET request only. Try loggin the request method in wildcard route itself.
– front_end_dev
Nov 11 at 4:43














Ok fair enough I see that now. Would you know of any other way to redirect only the undefined GET requests and not those that have an assigned app.post function?
– R.Snow
Nov 11 at 5:17




Ok fair enough I see that now. Would you know of any other way to redirect only the undefined GET requests and not those that have an assigned app.post function?
– R.Snow
Nov 11 at 5:17












Put this in the end of file app._router.stack.forEach(function(middleware){ if(middleware.route){ console.log(middleware.route); } }); It will print all the register routes on the app based on that you can create your logic to either redirect or not.
– front_end_dev
Nov 11 at 5:33




Put this in the end of file app._router.stack.forEach(function(middleware){ if(middleware.route){ console.log(middleware.route); } }); It will print all the register routes on the app based on that you can create your logic to either redirect or not.
– front_end_dev
Nov 11 at 5:33












Thanks for that. Your code successfully gives me a list of routes which I can compare incoming requests against. However the issue now is, I cannot correctly get the request path. None of the suggested req.originalUrl, req.url, req.baseUrl or req.path return the path on the browser request. I guess I'll have to ask a new question for that. But thanks anyway
– R.Snow
Nov 11 at 5:59






Thanks for that. Your code successfully gives me a list of routes which I can compare incoming requests against. However the issue now is, I cannot correctly get the request path. None of the suggested req.originalUrl, req.url, req.baseUrl or req.path return the path on the browser request. I guess I'll have to ask a new question for that. But thanks anyway
– R.Snow
Nov 11 at 5:59














2 Answers
2






active

oldest

votes

















up vote
0
down vote













If you are testing in the browser all the request by default will be a "GET" request and as you have said all your request is "POST" type so try with postman or other similar api testing tool






share|improve this answer





















  • Hi, thanks for that. Yes I've now realised even the POST requests are sent as GET requests by the browser and that's why the GET wildcard is picking up the redirects. Do you know of a way of implementing a redirect in Express.js only for undefined requests?
    – R.Snow
    Nov 11 at 5:23


















up vote
0
down vote













[SOLVED]



So the issue was found to be rogue secondary favicon requests by some browsers. The initial defined requests were indeed being picked up the by the respective route handler, however some browsers, such as Google Chrome tend to send secondary favicon requests, separate from the user initiated request and with letting the user know.



This was the request being undefined and picked up by the final wildcard router. Browsers that do not exhibit such rogue behaviour include Apple Safari on macOS and explain why this issue is not 100% reproducible.



Solution is to create an empty favicon router to handle such requests if the request if from a browser that exhibits such non-user initiated behaviour like Google Chrome.






share|improve this answer





















    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%2f53245861%2fwildcard-route-responding-to-all-requests%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    If you are testing in the browser all the request by default will be a "GET" request and as you have said all your request is "POST" type so try with postman or other similar api testing tool






    share|improve this answer





















    • Hi, thanks for that. Yes I've now realised even the POST requests are sent as GET requests by the browser and that's why the GET wildcard is picking up the redirects. Do you know of a way of implementing a redirect in Express.js only for undefined requests?
      – R.Snow
      Nov 11 at 5:23















    up vote
    0
    down vote













    If you are testing in the browser all the request by default will be a "GET" request and as you have said all your request is "POST" type so try with postman or other similar api testing tool






    share|improve this answer





















    • Hi, thanks for that. Yes I've now realised even the POST requests are sent as GET requests by the browser and that's why the GET wildcard is picking up the redirects. Do you know of a way of implementing a redirect in Express.js only for undefined requests?
      – R.Snow
      Nov 11 at 5:23













    up vote
    0
    down vote










    up vote
    0
    down vote









    If you are testing in the browser all the request by default will be a "GET" request and as you have said all your request is "POST" type so try with postman or other similar api testing tool






    share|improve this answer












    If you are testing in the browser all the request by default will be a "GET" request and as you have said all your request is "POST" type so try with postman or other similar api testing tool







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 at 5:17









    Agam kumar

    11




    11












    • Hi, thanks for that. Yes I've now realised even the POST requests are sent as GET requests by the browser and that's why the GET wildcard is picking up the redirects. Do you know of a way of implementing a redirect in Express.js only for undefined requests?
      – R.Snow
      Nov 11 at 5:23


















    • Hi, thanks for that. Yes I've now realised even the POST requests are sent as GET requests by the browser and that's why the GET wildcard is picking up the redirects. Do you know of a way of implementing a redirect in Express.js only for undefined requests?
      – R.Snow
      Nov 11 at 5:23
















    Hi, thanks for that. Yes I've now realised even the POST requests are sent as GET requests by the browser and that's why the GET wildcard is picking up the redirects. Do you know of a way of implementing a redirect in Express.js only for undefined requests?
    – R.Snow
    Nov 11 at 5:23




    Hi, thanks for that. Yes I've now realised even the POST requests are sent as GET requests by the browser and that's why the GET wildcard is picking up the redirects. Do you know of a way of implementing a redirect in Express.js only for undefined requests?
    – R.Snow
    Nov 11 at 5:23












    up vote
    0
    down vote













    [SOLVED]



    So the issue was found to be rogue secondary favicon requests by some browsers. The initial defined requests were indeed being picked up the by the respective route handler, however some browsers, such as Google Chrome tend to send secondary favicon requests, separate from the user initiated request and with letting the user know.



    This was the request being undefined and picked up by the final wildcard router. Browsers that do not exhibit such rogue behaviour include Apple Safari on macOS and explain why this issue is not 100% reproducible.



    Solution is to create an empty favicon router to handle such requests if the request if from a browser that exhibits such non-user initiated behaviour like Google Chrome.






    share|improve this answer

























      up vote
      0
      down vote













      [SOLVED]



      So the issue was found to be rogue secondary favicon requests by some browsers. The initial defined requests were indeed being picked up the by the respective route handler, however some browsers, such as Google Chrome tend to send secondary favicon requests, separate from the user initiated request and with letting the user know.



      This was the request being undefined and picked up by the final wildcard router. Browsers that do not exhibit such rogue behaviour include Apple Safari on macOS and explain why this issue is not 100% reproducible.



      Solution is to create an empty favicon router to handle such requests if the request if from a browser that exhibits such non-user initiated behaviour like Google Chrome.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        [SOLVED]



        So the issue was found to be rogue secondary favicon requests by some browsers. The initial defined requests were indeed being picked up the by the respective route handler, however some browsers, such as Google Chrome tend to send secondary favicon requests, separate from the user initiated request and with letting the user know.



        This was the request being undefined and picked up by the final wildcard router. Browsers that do not exhibit such rogue behaviour include Apple Safari on macOS and explain why this issue is not 100% reproducible.



        Solution is to create an empty favicon router to handle such requests if the request if from a browser that exhibits such non-user initiated behaviour like Google Chrome.






        share|improve this answer












        [SOLVED]



        So the issue was found to be rogue secondary favicon requests by some browsers. The initial defined requests were indeed being picked up the by the respective route handler, however some browsers, such as Google Chrome tend to send secondary favicon requests, separate from the user initiated request and with letting the user know.



        This was the request being undefined and picked up by the final wildcard router. Browsers that do not exhibit such rogue behaviour include Apple Safari on macOS and explain why this issue is not 100% reproducible.



        Solution is to create an empty favicon router to handle such requests if the request if from a browser that exhibits such non-user initiated behaviour like Google Chrome.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 6:57









        R.Snow

        2115




        2115






























            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%2f53245861%2fwildcard-route-responding-to-all-requests%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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)