Is it possible to use multiple .get on app.route? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do I get started with Node.jsHow do I get the path to the current script with Node.js?How to get GET (query string) variables in Express.js on Node.js?How to allow CORS?How do I update/upsert a document in Mongoose?Node.js / Express.js - How does app.router work?Express.js middleware branching or multiple app.routes middlewaresNode js ECONNRESETIs it not possible to stringify an Error using JSON.stringify?sending multiple JSON Objects lists in node js

Most effective melee weapons for arboreal combat? (pre-gunpowder technology)

Project Euler #1 in C++

In musical terms, what properties are varied by the human voice to produce different words / syllables?

Getting out of while loop on console

Did pre-Columbian Americans know the spherical shape of the Earth?

Co-worker has annoying ringtone

Was Kant an Intuitionist about mathematical objects?

Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?

Rationale for describing kurtosis as "peakedness"?

Why datecode is SO IMPORTANT to chip manufacturers?

How to change the tick of the color bar legend to black

What does Turing mean by this statement?

How many time has Arya actually used Needle?

Can two people see the same photon?

Trying to understand entropy as a novice in thermodynamics

A proverb that is used to imply that you have unexpectedly faced a big problem

What does 丫 mean? 丫是什么意思?

What would you call this weird metallic apparatus that allows you to lift people?

One-one communication

Why complex landing gears are used instead of simple,reliability and light weight muscle wire or shape memory alloys?

Asymptotics question

How can a team of shapeshifters communicate?

What is a more techy Technical Writer job title that isn't cutesy or confusing?

Does the Mueller report show a conspiracy between Russia and the Trump Campaign?



Is it possible to use multiple .get on app.route?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do I get started with Node.jsHow do I get the path to the current script with Node.js?How to get GET (query string) variables in Express.js on Node.js?How to allow CORS?How do I update/upsert a document in Mongoose?Node.js / Express.js - How does app.router work?Express.js middleware branching or multiple app.routes middlewaresNode js ECONNRESETIs it not possible to stringify an Error using JSON.stringify?sending multiple JSON Objects lists in node js



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















app.route('/users')
.post(user.post)
.get(user.get)
.get(user.everyone)
.put(user.update)
.delete(user.delete);


I have ran into the problem of my function using two res.send, so I am getting the 'Error: cannot set header after they are sent.' error, to fix this I have turned it into two functions which I am trying to use two .get on the app.route, but it seems I can only use one as when I use two the second one doesn't work.



Is there a way I could use two .get on one app.route?
If not, what are my options to get around this problem?










share|improve this question






















  • /users will have at most one route of each kind of method. To have two get use /users/:id and /users for get single user by id and get users list.

    – Ankit Agarwal
    Mar 22 at 11:51

















0















app.route('/users')
.post(user.post)
.get(user.get)
.get(user.everyone)
.put(user.update)
.delete(user.delete);


I have ran into the problem of my function using two res.send, so I am getting the 'Error: cannot set header after they are sent.' error, to fix this I have turned it into two functions which I am trying to use two .get on the app.route, but it seems I can only use one as when I use two the second one doesn't work.



Is there a way I could use two .get on one app.route?
If not, what are my options to get around this problem?










share|improve this question






















  • /users will have at most one route of each kind of method. To have two get use /users/:id and /users for get single user by id and get users list.

    – Ankit Agarwal
    Mar 22 at 11:51













0












0








0








app.route('/users')
.post(user.post)
.get(user.get)
.get(user.everyone)
.put(user.update)
.delete(user.delete);


I have ran into the problem of my function using two res.send, so I am getting the 'Error: cannot set header after they are sent.' error, to fix this I have turned it into two functions which I am trying to use two .get on the app.route, but it seems I can only use one as when I use two the second one doesn't work.



Is there a way I could use two .get on one app.route?
If not, what are my options to get around this problem?










share|improve this question














app.route('/users')
.post(user.post)
.get(user.get)
.get(user.everyone)
.put(user.update)
.delete(user.delete);


I have ran into the problem of my function using two res.send, so I am getting the 'Error: cannot set header after they are sent.' error, to fix this I have turned it into two functions which I am trying to use two .get on the app.route, but it seems I can only use one as when I use two the second one doesn't work.



Is there a way I could use two .get on one app.route?
If not, what are my options to get around this problem?







node.js express






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 11:49









DisplayNameDisplayName

184




184












  • /users will have at most one route of each kind of method. To have two get use /users/:id and /users for get single user by id and get users list.

    – Ankit Agarwal
    Mar 22 at 11:51

















  • /users will have at most one route of each kind of method. To have two get use /users/:id and /users for get single user by id and get users list.

    – Ankit Agarwal
    Mar 22 at 11:51
















/users will have at most one route of each kind of method. To have two get use /users/:id and /users for get single user by id and get users list.

– Ankit Agarwal
Mar 22 at 11:51





/users will have at most one route of each kind of method. To have two get use /users/:id and /users for get single user by id and get users list.

– Ankit Agarwal
Mar 22 at 11:51












3 Answers
3






active

oldest

votes


















0














You need to create separate routes for each api endpoint like this:



app.route('/users').get(req, res) => 
//Get all users
);

app.route('/users:/id').get(req, res) =>
//Get specific user
);

app.route('/users').post(req, res) =>
//Create new user
);

app.route('/users/:id').put(req, res) =>
//Update user
);

app.route('/users/:id').delete(req, res) =>
//Delete user
);





share|improve this answer























  • and if I am not wrong, instead of sending the response (res.send()), you can call next() and i think it will pass to next get()

    – Vinay Sheoran
    Mar 22 at 12:01


















0














Once res.send is called, means your server has sent the response to the browser or whatever. you can't change the already sent response and its header.



You can use multiple callbacks on one route and one method(post,get)



An array of callback functions can handle a route. For example:



var cb0 = function (req, res, next) 
console.log('CB0')
next()


var cb1 = function (req, res, next)
console.log('CB1')
next()


var cb2 = function (req, res)
res.send('Hello from C!')


app.get('/example/c', [cb0, cb1, cb2])





share|improve this answer


















  • 1





    This is a middleware implementation. Business logic shouldnt be applied like this, instead, if there are several steps that need to be implemented in sequence, we should should promises / async-await logic inside the route callback

    – Kieran Quinn
    Mar 22 at 12:13


















0














yes, you can use multiple HTTP requests either its .get or .post but with different params. or routes.






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',
    autoActivateHeartbeat: false,
    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%2f55298969%2fis-it-possible-to-use-multiple-get-on-app-route%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









    0














    You need to create separate routes for each api endpoint like this:



    app.route('/users').get(req, res) => 
    //Get all users
    );

    app.route('/users:/id').get(req, res) =>
    //Get specific user
    );

    app.route('/users').post(req, res) =>
    //Create new user
    );

    app.route('/users/:id').put(req, res) =>
    //Update user
    );

    app.route('/users/:id').delete(req, res) =>
    //Delete user
    );





    share|improve this answer























    • and if I am not wrong, instead of sending the response (res.send()), you can call next() and i think it will pass to next get()

      – Vinay Sheoran
      Mar 22 at 12:01















    0














    You need to create separate routes for each api endpoint like this:



    app.route('/users').get(req, res) => 
    //Get all users
    );

    app.route('/users:/id').get(req, res) =>
    //Get specific user
    );

    app.route('/users').post(req, res) =>
    //Create new user
    );

    app.route('/users/:id').put(req, res) =>
    //Update user
    );

    app.route('/users/:id').delete(req, res) =>
    //Delete user
    );





    share|improve this answer























    • and if I am not wrong, instead of sending the response (res.send()), you can call next() and i think it will pass to next get()

      – Vinay Sheoran
      Mar 22 at 12:01













    0












    0








    0







    You need to create separate routes for each api endpoint like this:



    app.route('/users').get(req, res) => 
    //Get all users
    );

    app.route('/users:/id').get(req, res) =>
    //Get specific user
    );

    app.route('/users').post(req, res) =>
    //Create new user
    );

    app.route('/users/:id').put(req, res) =>
    //Update user
    );

    app.route('/users/:id').delete(req, res) =>
    //Delete user
    );





    share|improve this answer













    You need to create separate routes for each api endpoint like this:



    app.route('/users').get(req, res) => 
    //Get all users
    );

    app.route('/users:/id').get(req, res) =>
    //Get specific user
    );

    app.route('/users').post(req, res) =>
    //Create new user
    );

    app.route('/users/:id').put(req, res) =>
    //Update user
    );

    app.route('/users/:id').delete(req, res) =>
    //Delete user
    );






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 22 at 11:57









    Kieran QuinnKieran Quinn

    410823




    410823












    • and if I am not wrong, instead of sending the response (res.send()), you can call next() and i think it will pass to next get()

      – Vinay Sheoran
      Mar 22 at 12:01

















    • and if I am not wrong, instead of sending the response (res.send()), you can call next() and i think it will pass to next get()

      – Vinay Sheoran
      Mar 22 at 12:01
















    and if I am not wrong, instead of sending the response (res.send()), you can call next() and i think it will pass to next get()

    – Vinay Sheoran
    Mar 22 at 12:01





    and if I am not wrong, instead of sending the response (res.send()), you can call next() and i think it will pass to next get()

    – Vinay Sheoran
    Mar 22 at 12:01













    0














    Once res.send is called, means your server has sent the response to the browser or whatever. you can't change the already sent response and its header.



    You can use multiple callbacks on one route and one method(post,get)



    An array of callback functions can handle a route. For example:



    var cb0 = function (req, res, next) 
    console.log('CB0')
    next()


    var cb1 = function (req, res, next)
    console.log('CB1')
    next()


    var cb2 = function (req, res)
    res.send('Hello from C!')


    app.get('/example/c', [cb0, cb1, cb2])





    share|improve this answer


















    • 1





      This is a middleware implementation. Business logic shouldnt be applied like this, instead, if there are several steps that need to be implemented in sequence, we should should promises / async-await logic inside the route callback

      – Kieran Quinn
      Mar 22 at 12:13















    0














    Once res.send is called, means your server has sent the response to the browser or whatever. you can't change the already sent response and its header.



    You can use multiple callbacks on one route and one method(post,get)



    An array of callback functions can handle a route. For example:



    var cb0 = function (req, res, next) 
    console.log('CB0')
    next()


    var cb1 = function (req, res, next)
    console.log('CB1')
    next()


    var cb2 = function (req, res)
    res.send('Hello from C!')


    app.get('/example/c', [cb0, cb1, cb2])





    share|improve this answer


















    • 1





      This is a middleware implementation. Business logic shouldnt be applied like this, instead, if there are several steps that need to be implemented in sequence, we should should promises / async-await logic inside the route callback

      – Kieran Quinn
      Mar 22 at 12:13













    0












    0








    0







    Once res.send is called, means your server has sent the response to the browser or whatever. you can't change the already sent response and its header.



    You can use multiple callbacks on one route and one method(post,get)



    An array of callback functions can handle a route. For example:



    var cb0 = function (req, res, next) 
    console.log('CB0')
    next()


    var cb1 = function (req, res, next)
    console.log('CB1')
    next()


    var cb2 = function (req, res)
    res.send('Hello from C!')


    app.get('/example/c', [cb0, cb1, cb2])





    share|improve this answer













    Once res.send is called, means your server has sent the response to the browser or whatever. you can't change the already sent response and its header.



    You can use multiple callbacks on one route and one method(post,get)



    An array of callback functions can handle a route. For example:



    var cb0 = function (req, res, next) 
    console.log('CB0')
    next()


    var cb1 = function (req, res, next)
    console.log('CB1')
    next()


    var cb2 = function (req, res)
    res.send('Hello from C!')


    app.get('/example/c', [cb0, cb1, cb2])






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 22 at 11:58









    Vinay SheoranVinay Sheoran

    328310




    328310







    • 1





      This is a middleware implementation. Business logic shouldnt be applied like this, instead, if there are several steps that need to be implemented in sequence, we should should promises / async-await logic inside the route callback

      – Kieran Quinn
      Mar 22 at 12:13












    • 1





      This is a middleware implementation. Business logic shouldnt be applied like this, instead, if there are several steps that need to be implemented in sequence, we should should promises / async-await logic inside the route callback

      – Kieran Quinn
      Mar 22 at 12:13







    1




    1





    This is a middleware implementation. Business logic shouldnt be applied like this, instead, if there are several steps that need to be implemented in sequence, we should should promises / async-await logic inside the route callback

    – Kieran Quinn
    Mar 22 at 12:13





    This is a middleware implementation. Business logic shouldnt be applied like this, instead, if there are several steps that need to be implemented in sequence, we should should promises / async-await logic inside the route callback

    – Kieran Quinn
    Mar 22 at 12:13











    0














    yes, you can use multiple HTTP requests either its .get or .post but with different params. or routes.






    share|improve this answer



























      0














      yes, you can use multiple HTTP requests either its .get or .post but with different params. or routes.






      share|improve this answer

























        0












        0








        0







        yes, you can use multiple HTTP requests either its .get or .post but with different params. or routes.






        share|improve this answer













        yes, you can use multiple HTTP requests either its .get or .post but with different params. or routes.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 22 at 13:17









        fahad tufailfahad tufail

        815




        815



























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55298969%2fis-it-possible-to-use-multiple-get-on-app-route%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript