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?
node.js express
add a comment |
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?
node.js express
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
add a comment |
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?
node.js express
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
node.js express
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
add a comment |
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
add a comment |
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);
);
add a comment |
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.
add a comment |
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
add a comment |
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);
);
add a comment |
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);
);
add a comment |
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);
);
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);
);
edited Nov 10 at 16:54
answered Nov 10 at 16:49
pzaenger
2,73311625
2,73311625
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 10 at 17:03
jfriend00
421k48533579
421k48533579
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 10 at 17:06
front_end_dev
1,3101411
1,3101411
add a comment |
add a comment |
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%2f53241157%2fhow-to-add-query-parameters-to-router-get%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
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