Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 140 Vote(s) - 3.29 Average
  • 1
  • 2
  • 3
  • 4
  • 5
node.js with express how to remove the query string from the url

#11
## 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();
});
```





Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through