the request url is /validations/2?username=sdfsf&c=3
it is expected that req.baseUrl will be /validations/2, but got nothing.
Hi! We need some more information, please :) Can you provide your code that has this issue and the version of Express you are using?
express routing:
app.all('/aa/:id', function (req, res) {
console.log(req.baseUrl, req.url);
res.end();
});
testing code:
it('should post /aa/:id', function (done) {
request(app)
.post('/aa/1')
.expect(200)
.end(function (err, res) {
assert(!err);
// assert(res.text === 'Access Denied!');
done();
});
});
actual result:
undefined
/aa/1
expected string:
/aa/:id
I have tried express 4.0.4 and 4.14.0
got the same result.
In your example, undefined for req.baseUrl is how it is supposed to work. Perhaps the documentation is just not clear on how that property works.
What you are expecting is not provided by Express currently, but you are welcome to make a PR to add a feature to do that :)
Closing since this is working as designed.
@dougwison
where can i get /aa/:id ?
What you are expecting is not provided by Express currently, but you are welcome to make a PR to add a feature to do that :)
@dougwilson
it seems the new code base has added this feature?
this req.route.path seems not working for middleware.
it seems middleware should be prioritized to separate user's middleware from the core middleware,.
Hi @calidion sorry I could have been clearer, I apologize. Your example:
app.use('/aa/:id', function (req, res) {
console.log(req.baseUrl, req.url);
res.end();
});
Is creating a middleware that will handle any request that starts with /aa/* (like /aa/1/foo/bar) and fill in the req.params.id with the * part ('1' in the example).
The req.baseUrl (http://expressjs.com/en/4x/api.html#req.baseUrl) contains the base path a router instance was mounted on. Since your example doesn't involve mounting any routers, it would be undefined.
The req.route (http://expressjs.com/en/4x/api.html#req.route) will not be populated, because app.use is for mounting middleware, and isn't itself a route (a route is when you use app[METHOD] or app.all syntax).
@dougwilson
sorry, i have correct my code. it should not be use, it should be get, post, all or some other http methods.
and I made a hack on get the routing pattern according to the url. but haven't tested in cases where a Router object is added. it seems that the pattern I requested is not that easy to be implemented.
I have investigated some expressjs code to know that the layers are the places to handle such pattern matching. but i am not sure if it still works if more nested routers are added.