Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

req.baseUrl is empty

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.

like image 766
boron.9 Avatar asked Aug 06 '26 12:08

boron.9


1 Answers

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.

like image 119
gregooroo Avatar answered Aug 08 '26 02:08

gregooroo