07-21-2023, 02:31 PM
## Generic Node.js solution
```node.js
> new URL('https://example.com/admin/new?foo=bar').pathname;
'/admin/new'
```
If you also want to include the origin:
```node.js
> const url = new URL('https://example.com/admin/new?foo=bar')
> url.search = '';
> url.toString()
'https://example.com/admin/new'
```
## Express.js solution
Other answers include various combinations of using `req.url` / `req.originalUrl` / `req.path` / `req.baseUrl`. Which one to use depends on the context. These properties behave differently if you're in the `app.METHOD` function or if you're accessing them from inside a middleware or a nested router.
**Summary:**
- Using `req.originalUrl` should be the most versatile, but you need to parse the pathname yourself (see above).
- Using `req.path` is the most straightforward, but you have to make sure you're not in a middleware/nested router.
#### When you're inside of the `app.METHOD` function:
```node.js
// GET 'http://example.com/admin/new?foo=bar'
app.get('/admin/new', (req, res) => {
console.dir(req.originalUrl); // '/admin/new?foo=bar'
console.dir(req.url); // '/admin/new?foo=bar
console.dir(req.baseUrl); // ''
console.dir(req.path); // '/admin/new'
return res.status(200).send('All engines running!');
});
```
#### When you're inside a middleware (or a nested router):
```node.js
// GET 'http://example.com/admin/new?foo=bar'
app.use('/admin', (req, res, next) => {
console.dir(req.originalUrl); // '/admin/new?foo=bar'
console.dir(req.url); // '/new?foo=bar'
console.dir(req.baseUrl); // '/admin'
console.dir(req.path); // '/new'
next();
});
```
```node.js
> new URL('https://example.com/admin/new?foo=bar').pathname;
'/admin/new'
```
If you also want to include the origin:
```node.js
> const url = new URL('https://example.com/admin/new?foo=bar')
> url.search = '';
> url.toString()
'https://example.com/admin/new'
```
## Express.js solution
Other answers include various combinations of using `req.url` / `req.originalUrl` / `req.path` / `req.baseUrl`. Which one to use depends on the context. These properties behave differently if you're in the `app.METHOD` function or if you're accessing them from inside a middleware or a nested router.
**Summary:**
- Using `req.originalUrl` should be the most versatile, but you need to parse the pathname yourself (see above).
- Using `req.path` is the most straightforward, but you have to make sure you're not in a middleware/nested router.
#### When you're inside of the `app.METHOD` function:
```node.js
// GET 'http://example.com/admin/new?foo=bar'
app.get('/admin/new', (req, res) => {
console.dir(req.originalUrl); // '/admin/new?foo=bar'
console.dir(req.url); // '/admin/new?foo=bar
console.dir(req.baseUrl); // ''
console.dir(req.path); // '/admin/new'
return res.status(200).send('All engines running!');
});
```
#### When you're inside a middleware (or a nested router):
```node.js
// GET 'http://example.com/admin/new?foo=bar'
app.use('/admin', (req, res, next) => {
console.dir(req.originalUrl); // '/admin/new?foo=bar'
console.dir(req.url); // '/new?foo=bar'
console.dir(req.baseUrl); // '/admin'
console.dir(req.path); // '/new'
next();
});
```