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

#1
I have a button that is performing a get to my page and adding a filter to the query string. My code applies that filter to the grid but the user can remove/edit that filter. Since they can see what filter was applied in the grid, I would like to remove the `?filter=blah` from the query string when the page is displayed.

It might be confusing if on the page and the URL says `?filter=columnA` which is correct initially, but the user removes that filter and applies a new one on `columnB` but the query string still says `?filter-columnA`. The grid can handle changing filters without needing a post back.

How can I do that? And if you cannot remove/update a query string, is it possible to parse it and then just redirect to the main page without the query string? Once I have the filter saved to var filter, I no longer need it in the query string.

here is the code that displays the page:

exports.show = function(req, res) {
var filter = req.query.filter;
if (filter === null || filter === "") {
filter = "n/a";
}

res.render("somepage.jade", {
locals: {
title: "somepage",
filter: filter
}
});

};
Reply

#2
The full url is stored in `req.url` in your case, use node.js's [url.parse()][1] to pull out the parts. Take the path and send a Location header using `res.set()` to redirect to URL without the query string.

var url = require('url');
res.set('Location', url.parse(req.url).pathname);

[1]:

[To see links please register here]

Reply

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

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

#4
// load built-in utilities for URL resolution and parsing
var url = require('url');

function removeQueryString(url){

// split url into distinct parts
// (full list:

[To see links please register here]

)
var obj = url.parse(url);

// remove the querystring
obj.search = obj.query = "";

// reassemble the url
return url.format(obj);

}
Reply

#5
Express 4.x+ answer:
`res.redirect(req.path)`
Reply

#6
In order to avoid reload the page by forcing a redirect, I added the following to the `<head>` section of my .ejs file:

<script type="text/javascript">
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, clean_uri);
}
</script>

Source:

[To see links please register here]


Reply

#7
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 utility
res.redirect(url.parse(req.url).pathname);

**Node** docs for [`url`][1].


[1]:

[To see links please register here]

Reply

#8
I had a similar issue and the way that I approached it was by adding a script in the **<head>** section. However, in order to avoid inconsistencies when I was moving either backward or forward I needed to add an **onbeforeunload** event listener. The benefit of that approach is that it avoids the redirection.

<pre>

// Stores the original url in the local storage
window.localStorage.setItem('specifiedKey', window.location.href);

// Cleans the query parameter of a string and replace it in the history API
const cleanUrl = location.href.match(/^.+(?=\?)/g);
window.history.replaceState(null, null, (cleanUrl ? cleanUrl[0] : location.href));

// the history is updated before the window reloads
window.onbeforeunload = () => {
window.history.replaceState(null, null, window.localStorage.getItem('specifiedKey'));
}

</pre>

The only issue that I imagine is browser incompatibility, with the JavaScript engine not being able to support a regex look-behind operator. This can be easily fixed using .split('?')[0]
Reply

#9
use this method to remove specific query parameter from URL.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

/**
* remove query parameters from actual url
* @param {*} params paramerters to be remove, e.g ['foo', 'bar']
* @param {*} url actual url
*/
function removeQueryParam(parameters = [], url) {
try {
var urlParts = url.split('?');
var params = new URLSearchParams(urlParts[1]);
parameters.forEach(param => {
params.delete(param);
})
return urlParts[0] + '?' + params.toString();
} catch (err) {
console.log(err);
return url;
}
}

console.log(removeQueryParam(["foo"], "/foo?foo=foo&bar=bar"));

<!-- end snippet -->


Above example will return **/foo?bar=bar**
Reply

#10
**Use `req.path`**

If your endpoint is `http://<your-domain>.com/hello/there?name=john`...

then `req.path` = `/hello/there`


----------


Documentation:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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