I am using the express.js
framework on node.js
. I am trying to get the value of the first string argument from app.get()
from within the callback.
app.get('/example/code', function(req,res){
console.log(firstParam); // "/example/code"
});
app.get('/example/:id', function(req,res){
console.log(firstParam); // "/example/:id"
});
app.get('/example(s?)', function(req,res){
console.log(firstParam); // "/example(s?)"
});
Any way to do this? As you can see I don't want the url exactly, I want what the factor for the route was. For the second example I DO NOT WANT to return /example/2
You can get the path inside the callback with:
req.route.path
Among other info out of req.route
:
{ path: '/example/:id',
method: 'get',
callbacks: [ [Function] ],
keys: [ { name: 'id', optional: false } ],
regexp: /^\/example\/(?:([^\/]+?))\/?$/i,
params: [ id: '42' ] }