Express - get value of path matching string The 2019 Stack Overflow Developer Survey Results Are InHow do I get started with Node.jsHow do I get the path to the current script with Node.js?Convert JS object to JSON stringHow to get GET (query string) variables in Express.js on Node.js?How to get the full url in Express?How to access the GET parameters after “?” in Express?& in http get requestWhy this error coming while running nodejs server?In express, why app.use function can use route argument?Why express.Router() while separating routes
Do these rules for Critical Successes and Critical Failures seem Fair?
Loose spokes after only a few rides
Delete all lines which don't have n characters before delimiter
What tool would a Roman-age civilization have for the breaking of silver and other metals into dust?
Can a rogue use sneak attack with weapons that have the thrown property even if they are not thrown?
During Temple times, who can butcher a kosher animal?
Apparent duplicates between Haynes service instructions and MOT
Did Section 31 appear in Star Trek: The Next Generation?
Why can Shazam fly?
Does the shape of a die affect the probability of a number being rolled?
Why isn't airport relocation done gradually?
FPGA - DIY Programming
A poker game description that does not feel gimmicky
What is the meaning of the verb "bear" in this context?
What do hard-Brexiteers want with respect to the Irish border?
What is the meaning of Triage in Cybersec world?
Can one be advised by a professor who is very far away?
Origin of "cooter" meaning "vagina"
How to save as into a customized destination on macOS?
How to answer pointed "are you quitting" questioning when I don't want them to suspect
Multiply Two Integer Polynomials
Protecting Dualbooting Windows from dangerous code (like rm -rf)
The difference between dialogue marks
What to do when moving next to a bird sanctuary with a loosely-domesticated cat?
Express - get value of path matching string
The 2019 Stack Overflow Developer Survey Results Are InHow do I get started with Node.jsHow do I get the path to the current script with Node.js?Convert JS object to JSON stringHow to get GET (query string) variables in Express.js on Node.js?How to get the full url in Express?How to access the GET parameters after “?” in Express?& in http get requestWhy this error coming while running nodejs server?In express, why app.use function can use route argument?Why express.Router() while separating routes
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Question
I've got several nested routers, and would like to get access to the whole string that the request's path matched. It's a little hard to say with english, so take a look at this code:
const express = require('express')
const app = express()
const router1 = express.Router()
const router2 = express.Router()
// set up router 2 paths
router2.get('/path2/:param2', (req, res, next) =>
const someVar = req.something // the value I'll talk about in a second
return res.status(200).send( someVar )
)
// set up router1 paths
router1.use('/path1/:param1', router2)
// connect the routers behind a base url
app.use('/api/v1', router1)
app.listen(3000, function ()
console.log('Example app listening on port 3000!')
)
If I were to make a GET request with:
curl -X GET http://localhost:3000/api/v1/path1/myparam1/path2/myparam2
I want that to return an object like this:
"someVar": "/api/v1/path1/:param1/path2/:param2"
Context
I have middleware in my app which logs the path to an elasticsearch cluster, and I'd like the cluster to group paths by the string they used to match the request, rather than the request itself. That way I can get a visual of which request endpoints are being hit the most.
node.js express
add a comment |
Question
I've got several nested routers, and would like to get access to the whole string that the request's path matched. It's a little hard to say with english, so take a look at this code:
const express = require('express')
const app = express()
const router1 = express.Router()
const router2 = express.Router()
// set up router 2 paths
router2.get('/path2/:param2', (req, res, next) =>
const someVar = req.something // the value I'll talk about in a second
return res.status(200).send( someVar )
)
// set up router1 paths
router1.use('/path1/:param1', router2)
// connect the routers behind a base url
app.use('/api/v1', router1)
app.listen(3000, function ()
console.log('Example app listening on port 3000!')
)
If I were to make a GET request with:
curl -X GET http://localhost:3000/api/v1/path1/myparam1/path2/myparam2
I want that to return an object like this:
"someVar": "/api/v1/path1/:param1/path2/:param2"
Context
I have middleware in my app which logs the path to an elasticsearch cluster, and I'd like the cluster to group paths by the string they used to match the request, rather than the request itself. That way I can get a visual of which request endpoints are being hit the most.
node.js express
add a comment |
Question
I've got several nested routers, and would like to get access to the whole string that the request's path matched. It's a little hard to say with english, so take a look at this code:
const express = require('express')
const app = express()
const router1 = express.Router()
const router2 = express.Router()
// set up router 2 paths
router2.get('/path2/:param2', (req, res, next) =>
const someVar = req.something // the value I'll talk about in a second
return res.status(200).send( someVar )
)
// set up router1 paths
router1.use('/path1/:param1', router2)
// connect the routers behind a base url
app.use('/api/v1', router1)
app.listen(3000, function ()
console.log('Example app listening on port 3000!')
)
If I were to make a GET request with:
curl -X GET http://localhost:3000/api/v1/path1/myparam1/path2/myparam2
I want that to return an object like this:
"someVar": "/api/v1/path1/:param1/path2/:param2"
Context
I have middleware in my app which logs the path to an elasticsearch cluster, and I'd like the cluster to group paths by the string they used to match the request, rather than the request itself. That way I can get a visual of which request endpoints are being hit the most.
node.js express
Question
I've got several nested routers, and would like to get access to the whole string that the request's path matched. It's a little hard to say with english, so take a look at this code:
const express = require('express')
const app = express()
const router1 = express.Router()
const router2 = express.Router()
// set up router 2 paths
router2.get('/path2/:param2', (req, res, next) =>
const someVar = req.something // the value I'll talk about in a second
return res.status(200).send( someVar )
)
// set up router1 paths
router1.use('/path1/:param1', router2)
// connect the routers behind a base url
app.use('/api/v1', router1)
app.listen(3000, function ()
console.log('Example app listening on port 3000!')
)
If I were to make a GET request with:
curl -X GET http://localhost:3000/api/v1/path1/myparam1/path2/myparam2
I want that to return an object like this:
"someVar": "/api/v1/path1/:param1/path2/:param2"
Context
I have middleware in my app which logs the path to an elasticsearch cluster, and I'd like the cluster to group paths by the string they used to match the request, rather than the request itself. That way I can get a visual of which request endpoints are being hit the most.
node.js express
node.js express
asked Mar 22 at 3:40
CorbfonCorbfon
1,8161519
1,8161519
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
// you need to set mergeParams: true on the router,
// if you want to access params from the parent router
var router1 = express.Router(mergeParams: true);
Ok! That's a great answer to part of the question. What would I log, then, if I wanted to get access to the parameter matching string? e.g./path1/:param1?
– Corbfon
Mar 22 at 5:41
you can get the original url using "req.url"
– Mahendra suthar
Mar 22 at 6:56
So in this case, if I implement themergeParamsoption,req.urlwould return/api/v1/path1/myparam1/path2/myparam2where I want the value used in express to match the path,/api/v1/path1/:param1/path2/:param2. Is there a way to get this without adding specialized middleware?
– Corbfon
Mar 22 at 20:59
mergeParams in no specialized midlleware..you can use "req.url" to get the full request path and mergeParams option is to preserve the req.params values from the parent router.
– Mahendra suthar
Mar 23 at 11:54
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%2f55292553%2fexpress-get-value-of-path-matching-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
// you need to set mergeParams: true on the router,
// if you want to access params from the parent router
var router1 = express.Router(mergeParams: true);
Ok! That's a great answer to part of the question. What would I log, then, if I wanted to get access to the parameter matching string? e.g./path1/:param1?
– Corbfon
Mar 22 at 5:41
you can get the original url using "req.url"
– Mahendra suthar
Mar 22 at 6:56
So in this case, if I implement themergeParamsoption,req.urlwould return/api/v1/path1/myparam1/path2/myparam2where I want the value used in express to match the path,/api/v1/path1/:param1/path2/:param2. Is there a way to get this without adding specialized middleware?
– Corbfon
Mar 22 at 20:59
mergeParams in no specialized midlleware..you can use "req.url" to get the full request path and mergeParams option is to preserve the req.params values from the parent router.
– Mahendra suthar
Mar 23 at 11:54
add a comment |
// you need to set mergeParams: true on the router,
// if you want to access params from the parent router
var router1 = express.Router(mergeParams: true);
Ok! That's a great answer to part of the question. What would I log, then, if I wanted to get access to the parameter matching string? e.g./path1/:param1?
– Corbfon
Mar 22 at 5:41
you can get the original url using "req.url"
– Mahendra suthar
Mar 22 at 6:56
So in this case, if I implement themergeParamsoption,req.urlwould return/api/v1/path1/myparam1/path2/myparam2where I want the value used in express to match the path,/api/v1/path1/:param1/path2/:param2. Is there a way to get this without adding specialized middleware?
– Corbfon
Mar 22 at 20:59
mergeParams in no specialized midlleware..you can use "req.url" to get the full request path and mergeParams option is to preserve the req.params values from the parent router.
– Mahendra suthar
Mar 23 at 11:54
add a comment |
// you need to set mergeParams: true on the router,
// if you want to access params from the parent router
var router1 = express.Router(mergeParams: true);
// you need to set mergeParams: true on the router,
// if you want to access params from the parent router
var router1 = express.Router(mergeParams: true);
answered Mar 22 at 4:02
Mahendra sutharMahendra suthar
241112
241112
Ok! That's a great answer to part of the question. What would I log, then, if I wanted to get access to the parameter matching string? e.g./path1/:param1?
– Corbfon
Mar 22 at 5:41
you can get the original url using "req.url"
– Mahendra suthar
Mar 22 at 6:56
So in this case, if I implement themergeParamsoption,req.urlwould return/api/v1/path1/myparam1/path2/myparam2where I want the value used in express to match the path,/api/v1/path1/:param1/path2/:param2. Is there a way to get this without adding specialized middleware?
– Corbfon
Mar 22 at 20:59
mergeParams in no specialized midlleware..you can use "req.url" to get the full request path and mergeParams option is to preserve the req.params values from the parent router.
– Mahendra suthar
Mar 23 at 11:54
add a comment |
Ok! That's a great answer to part of the question. What would I log, then, if I wanted to get access to the parameter matching string? e.g./path1/:param1?
– Corbfon
Mar 22 at 5:41
you can get the original url using "req.url"
– Mahendra suthar
Mar 22 at 6:56
So in this case, if I implement themergeParamsoption,req.urlwould return/api/v1/path1/myparam1/path2/myparam2where I want the value used in express to match the path,/api/v1/path1/:param1/path2/:param2. Is there a way to get this without adding specialized middleware?
– Corbfon
Mar 22 at 20:59
mergeParams in no specialized midlleware..you can use "req.url" to get the full request path and mergeParams option is to preserve the req.params values from the parent router.
– Mahendra suthar
Mar 23 at 11:54
Ok! That's a great answer to part of the question. What would I log, then, if I wanted to get access to the parameter matching string? e.g.
/path1/:param1?– Corbfon
Mar 22 at 5:41
Ok! That's a great answer to part of the question. What would I log, then, if I wanted to get access to the parameter matching string? e.g.
/path1/:param1?– Corbfon
Mar 22 at 5:41
you can get the original url using "req.url"
– Mahendra suthar
Mar 22 at 6:56
you can get the original url using "req.url"
– Mahendra suthar
Mar 22 at 6:56
So in this case, if I implement the
mergeParams option, req.url would return /api/v1/path1/myparam1/path2/myparam2 where I want the value used in express to match the path, /api/v1/path1/:param1/path2/:param2. Is there a way to get this without adding specialized middleware?– Corbfon
Mar 22 at 20:59
So in this case, if I implement the
mergeParams option, req.url would return /api/v1/path1/myparam1/path2/myparam2 where I want the value used in express to match the path, /api/v1/path1/:param1/path2/:param2. Is there a way to get this without adding specialized middleware?– Corbfon
Mar 22 at 20:59
mergeParams in no specialized midlleware..you can use "req.url" to get the full request path and mergeParams option is to preserve the req.params values from the parent router.
– Mahendra suthar
Mar 23 at 11:54
mergeParams in no specialized midlleware..you can use "req.url" to get the full request path and mergeParams option is to preserve the req.params values from the parent router.
– Mahendra suthar
Mar 23 at 11:54
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%2f55292553%2fexpress-get-value-of-path-matching-string%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