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;
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
add a comment |
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
/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
add a comment |
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
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
node.js express
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
add a comment |
/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
add a comment |
3 Answers
3
active
oldest
votes
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
);
and if I am not wrong, instead of sending the response (res.send()), you can callnext()
and i think it will pass to nextget()
– Vinay Sheoran
Mar 22 at 12:01
add a comment |
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])
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
add a comment |
yes, you can use multiple HTTP requests either its .get or .post but with different params. or routes.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%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
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
);
and if I am not wrong, instead of sending the response (res.send()), you can callnext()
and i think it will pass to nextget()
– Vinay Sheoran
Mar 22 at 12:01
add a comment |
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
);
and if I am not wrong, instead of sending the response (res.send()), you can callnext()
and i think it will pass to nextget()
– Vinay Sheoran
Mar 22 at 12:01
add a comment |
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
);
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
);
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 callnext()
and i think it will pass to nextget()
– Vinay Sheoran
Mar 22 at 12:01
add a comment |
and if I am not wrong, instead of sending the response (res.send()), you can callnext()
and i think it will pass to nextget()
– 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
add a comment |
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])
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
add a comment |
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])
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
add a comment |
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])
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])
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
add a comment |
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
add a comment |
yes, you can use multiple HTTP requests either its .get or .post but with different params. or routes.
add a comment |
yes, you can use multiple HTTP requests either its .get or .post but with different params. or routes.
add a comment |
yes, you can use multiple HTTP requests either its .get or .post but with different params. or routes.
yes, you can use multiple HTTP requests either its .get or .post but with different params. or routes.
answered Mar 22 at 13:17
fahad tufailfahad tufail
815
815
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55298969%2fis-it-possible-to-use-multiple-get-on-app-route%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
/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