how to add query parameters to router.get









up vote
1
down vote

favorite












Environment: MEAN tech stack



Hi, I want to add a query parameter to my router.get but I'm not sure how to define it.



Works like this right now:



http://test.com/path1/path2/1

router.get('/path1/path2/:userId', (req, res) => {
let route = `GET /path1/path2/$req.params.userId`;


I just want to add a search query parameter, would it be something like this?



http://test.com/path1/path2/1?q=test


And how would that get defined in the router.get?










share|improve this question





















  • It doesn't, it doesn't change the actual route. Did the request not arrive when you tried it?
    – jonrsharpe
    Nov 10 at 16:53











  • I'm still digging through the code and haven't made it that far yet.
    – Rod
    Nov 10 at 17:27














up vote
1
down vote

favorite












Environment: MEAN tech stack



Hi, I want to add a query parameter to my router.get but I'm not sure how to define it.



Works like this right now:



http://test.com/path1/path2/1

router.get('/path1/path2/:userId', (req, res) => {
let route = `GET /path1/path2/$req.params.userId`;


I just want to add a search query parameter, would it be something like this?



http://test.com/path1/path2/1?q=test


And how would that get defined in the router.get?










share|improve this question





















  • It doesn't, it doesn't change the actual route. Did the request not arrive when you tried it?
    – jonrsharpe
    Nov 10 at 16:53











  • I'm still digging through the code and haven't made it that far yet.
    – Rod
    Nov 10 at 17:27












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Environment: MEAN tech stack



Hi, I want to add a query parameter to my router.get but I'm not sure how to define it.



Works like this right now:



http://test.com/path1/path2/1

router.get('/path1/path2/:userId', (req, res) => {
let route = `GET /path1/path2/$req.params.userId`;


I just want to add a search query parameter, would it be something like this?



http://test.com/path1/path2/1?q=test


And how would that get defined in the router.get?










share|improve this question













Environment: MEAN tech stack



Hi, I want to add a query parameter to my router.get but I'm not sure how to define it.



Works like this right now:



http://test.com/path1/path2/1

router.get('/path1/path2/:userId', (req, res) => {
let route = `GET /path1/path2/$req.params.userId`;


I just want to add a search query parameter, would it be something like this?



http://test.com/path1/path2/1?q=test


And how would that get defined in the router.get?







node.js express






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 16:46









Rod

5,5601775135




5,5601775135











  • It doesn't, it doesn't change the actual route. Did the request not arrive when you tried it?
    – jonrsharpe
    Nov 10 at 16:53











  • I'm still digging through the code and haven't made it that far yet.
    – Rod
    Nov 10 at 17:27
















  • It doesn't, it doesn't change the actual route. Did the request not arrive when you tried it?
    – jonrsharpe
    Nov 10 at 16:53











  • I'm still digging through the code and haven't made it that far yet.
    – Rod
    Nov 10 at 17:27















It doesn't, it doesn't change the actual route. Did the request not arrive when you tried it?
– jonrsharpe
Nov 10 at 16:53





It doesn't, it doesn't change the actual route. Did the request not arrive when you tried it?
– jonrsharpe
Nov 10 at 16:53













I'm still digging through the code and haven't made it that far yet.
– Rod
Nov 10 at 17:27




I'm still digging through the code and haven't made it that far yet.
– Rod
Nov 10 at 17:27












3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










You do not need to add a query parameter to your route directly. Just keep /path1/path2/:userId.



Within your function you can then check, if a query parameter exists, here via req.query.q.



// http://test.com/path1/path2/1?q=test
router.get('/path1/path2/:userId', (req, res) =>
let route = `GET /path1/path2/$req.params.userId`;

// If http://test.com/path1/path2/1, req.query.q is undefined
console.log(req.params.userId, req.query.q);
);





share|improve this answer





























    up vote
    1
    down vote













    You use the req.query object to get query parameters.



    So, for the URL http://test.com/path1/path2/1?q=test, you could get the query parameter like this:



    router.get('/path1/path2/:userId', (req, res) => 
    console.log(req.params.userId); // "1"
    console.log(req.query.q); // "test"
    );


    Doc for req.query is here.






    share|improve this answer



























      up vote
      1
      down vote













      For this url http://test.com/path1/path2/1?q=test



      access path params = req.params.userId.



      access query params = req.query.q.



      Read more from Express documentation



      http://expressjs.com/de/api.html#req.query



      http://expressjs.com/de/api.html#req.params






      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%2f53241157%2fhow-to-add-query-parameters-to-router-get%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










        You do not need to add a query parameter to your route directly. Just keep /path1/path2/:userId.



        Within your function you can then check, if a query parameter exists, here via req.query.q.



        // http://test.com/path1/path2/1?q=test
        router.get('/path1/path2/:userId', (req, res) =>
        let route = `GET /path1/path2/$req.params.userId`;

        // If http://test.com/path1/path2/1, req.query.q is undefined
        console.log(req.params.userId, req.query.q);
        );





        share|improve this answer


























          up vote
          1
          down vote



          accepted










          You do not need to add a query parameter to your route directly. Just keep /path1/path2/:userId.



          Within your function you can then check, if a query parameter exists, here via req.query.q.



          // http://test.com/path1/path2/1?q=test
          router.get('/path1/path2/:userId', (req, res) =>
          let route = `GET /path1/path2/$req.params.userId`;

          // If http://test.com/path1/path2/1, req.query.q is undefined
          console.log(req.params.userId, req.query.q);
          );





          share|improve this answer
























            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            You do not need to add a query parameter to your route directly. Just keep /path1/path2/:userId.



            Within your function you can then check, if a query parameter exists, here via req.query.q.



            // http://test.com/path1/path2/1?q=test
            router.get('/path1/path2/:userId', (req, res) =>
            let route = `GET /path1/path2/$req.params.userId`;

            // If http://test.com/path1/path2/1, req.query.q is undefined
            console.log(req.params.userId, req.query.q);
            );





            share|improve this answer














            You do not need to add a query parameter to your route directly. Just keep /path1/path2/:userId.



            Within your function you can then check, if a query parameter exists, here via req.query.q.



            // http://test.com/path1/path2/1?q=test
            router.get('/path1/path2/:userId', (req, res) =>
            let route = `GET /path1/path2/$req.params.userId`;

            // If http://test.com/path1/path2/1, req.query.q is undefined
            console.log(req.params.userId, req.query.q);
            );






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 16:54

























            answered Nov 10 at 16:49









            pzaenger

            2,73311625




            2,73311625






















                up vote
                1
                down vote













                You use the req.query object to get query parameters.



                So, for the URL http://test.com/path1/path2/1?q=test, you could get the query parameter like this:



                router.get('/path1/path2/:userId', (req, res) => 
                console.log(req.params.userId); // "1"
                console.log(req.query.q); // "test"
                );


                Doc for req.query is here.






                share|improve this answer
























                  up vote
                  1
                  down vote













                  You use the req.query object to get query parameters.



                  So, for the URL http://test.com/path1/path2/1?q=test, you could get the query parameter like this:



                  router.get('/path1/path2/:userId', (req, res) => 
                  console.log(req.params.userId); // "1"
                  console.log(req.query.q); // "test"
                  );


                  Doc for req.query is here.






                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    You use the req.query object to get query parameters.



                    So, for the URL http://test.com/path1/path2/1?q=test, you could get the query parameter like this:



                    router.get('/path1/path2/:userId', (req, res) => 
                    console.log(req.params.userId); // "1"
                    console.log(req.query.q); // "test"
                    );


                    Doc for req.query is here.






                    share|improve this answer












                    You use the req.query object to get query parameters.



                    So, for the URL http://test.com/path1/path2/1?q=test, you could get the query parameter like this:



                    router.get('/path1/path2/:userId', (req, res) => 
                    console.log(req.params.userId); // "1"
                    console.log(req.query.q); // "test"
                    );


                    Doc for req.query is here.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 10 at 17:03









                    jfriend00

                    421k48533579




                    421k48533579




















                        up vote
                        1
                        down vote













                        For this url http://test.com/path1/path2/1?q=test



                        access path params = req.params.userId.



                        access query params = req.query.q.



                        Read more from Express documentation



                        http://expressjs.com/de/api.html#req.query



                        http://expressjs.com/de/api.html#req.params






                        share|improve this answer
























                          up vote
                          1
                          down vote













                          For this url http://test.com/path1/path2/1?q=test



                          access path params = req.params.userId.



                          access query params = req.query.q.



                          Read more from Express documentation



                          http://expressjs.com/de/api.html#req.query



                          http://expressjs.com/de/api.html#req.params






                          share|improve this answer






















                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            For this url http://test.com/path1/path2/1?q=test



                            access path params = req.params.userId.



                            access query params = req.query.q.



                            Read more from Express documentation



                            http://expressjs.com/de/api.html#req.query



                            http://expressjs.com/de/api.html#req.params






                            share|improve this answer












                            For this url http://test.com/path1/path2/1?q=test



                            access path params = req.params.userId.



                            access query params = req.query.q.



                            Read more from Express documentation



                            http://expressjs.com/de/api.html#req.query



                            http://expressjs.com/de/api.html#req.params







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 10 at 17:06









                            front_end_dev

                            1,3101411




                            1,3101411



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241157%2fhow-to-add-query-parameters-to-router-get%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







                                這個網誌中的熱門文章

                                How to read a connectionString WITH PROVIDER in .NET Core?

                                In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                                Museum of Modern and Contemporary Art of Trento and Rovereto