I'm building an Express API, the API endpoint is GET /tasks/:id. I need to use req.baseUrl. It should contain '/tasks' in this case, but it's empty.
Printing out the req object shows that req.originalUrl is correct but req.baseUrl is an empty string:
...
baseUrl: '',
originalUrl: '/tasks/608aa21aac412401305ba105',
...
What could be causing this? I could extract the baseUrl from the originalUrl but I'd rather not do that if I don't have to.
Based on your answer above that you're using the same structure like this here, it make sense that req.baseUrl is empty. Why? Let't check the docs
req.baseUrl - The URL path on which a router instance was mounted.
Since your project doesn't involve any route mounting it's ok that you see empty string. Take a look at users/routes.config.js. Routes there are configured with .get. post etc and then this function is being executed inside index.js file - no app.use (mounting) involved. Compare this to the example presented in the docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With