node.js with express how to remove the query string from the url node.js with express how to remove the query string from the url express express

node.js with express how to remove the query string from the url


Use url.parse() to get the components of your address, which is req.url. The url without the query string is stored in the pathname property.

Use express' redirect to send the new page address.

const url = require('url'); // built-in utilityres.redirect(url.parse(req.url).pathname);

Node docs for url.


Don't use a module for doing something like that:

res.redirect( req.originalUrl.split("?").shift() );


Express 4.x+ answer:
res.redirect(req.path)