node.js 与 express 如何从 url 中删除查询字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14166898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
node.js with express how to remove the query string from the url
提问by dan27
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.
我有一个按钮,它正在执行访问我的页面并向查询字符串添加过滤器。我的代码将该过滤器应用于网格...但用户可以删除/编辑该过滤器。由于他们可以看到在网格中应用了什么过滤器,我想在显示页面时从查询字符串中删除 ?filter=blah 。(如果在页面上和 URL 说 ?filter=columnA 最初是正确的,这可能会令人困惑,但用户删除了该过滤器并在 columnB 上应用了一个新过滤器......但查询字符串仍然显示 ?filter-columnA。网格可以处理更改过滤器而无需回发。)我该怎么做?如果您无法删除/更新查询字符串,是否可以解析它,然后在没有查询字符串的情况下重定向到主页?将过滤器保存到 var 过滤器后,查询字符串中不再需要它。
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
}
});
};
回答by Constantine Turtsevich
Use url.parse()to get the components of your address, which is req.url. The url without the query string is stored in the pathnameproperty.
使用url.parse()你的住址,这是组件req.url。没有查询字符串的 url 存储在pathname属性中。
Use express' redirectto send the new page address.
使用 express'redirect发送新的页面地址。
const url = require('url'); // built-in utility
res.redirect(url.parse(req.url).pathname);
Nodedocs for url.
的节点文档url。
回答by Tim
Don't use a module for doing something like that:
不要使用模块来做这样的事情:
res.redirect( req.originalUrl.split("?").shift() );
回答by AjaxLeung
Express 4.x+ answer:res.redirect(req.path)
表达 4.x+ 答案:res.redirect(req.path)
回答by Alexander Gonchiy
// load built-in utilities for URL resolution and parsing
var url = require('url');
function removeQueryString(url){
// split url into distinct parts
// (full list: https://nodejs.org/api/url.html#url_url_parse_urlstr_parsequerystring_slashesdenotehost)
var obj = url.parse(url);
// remove the querystring
obj.search = obj.query = "";
// reassemble the url
return url.format(obj);
}
回答by SamT
The full url is stored in req.urlin your case, use node.js's url.parse()to pull out the parts. Take the path and send a Location header using res.set()to redirect to URL without the query string.
完整的 url 存储在req.url您的案例中,使用 node.js 的url.parse()拉出部分。获取路径并发送一个 Location 标头,res.set()用于在没有查询字符串的情况下重定向到 URL。
var url = require('url');
res.set('Location', url.parse(req.url).pathname);
回答by Derek Soike
Use req.path
用 req.path
If your endpoint is http://<your-domain>.com/hello/there?name=john...
如果您的端点是http://<your-domain>.com/hello/there?name=john...
then req.path= /hello/there
那么req.path=/hello/there
Documetation: https://expressjs.com/en/api.html#req.path
文档:https://expressjs.com/en/api.html#req.path
回答by Incinerator
In order to avoid reload the page by forcing a redirect, I added the following to the <head>section of my .ejs file:
为了避免通过强制重定向重新加载页面,我在<head>.ejs 文件的部分添加了以下内容:
<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: http://atodorov.org/blog/2013/01/28/remove-query-string-with-javascript-and-html5/
来源:http: //atodorov.org/blog/2013/01/28/remove-query-string-with-javascript-and-html5/
回答by Marios Simou
I had a similar issue and the way that I approached it was by adding a script in the section. However, in order to avoid inconsistencies when I was moving either backward or forward I needed to add an onbeforeunloadevent listener. The benefit of that approach is that it avoids the redirection.
我有一个类似的问题,我处理它的方法是在 部分。但是,为了避免在向后或向前移动时出现不一致,我需要添加一个onbeforeunload事件侦听器。这种方法的好处是它避免了重定向。
// 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'));
}
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]
我想象的唯一问题是浏览器不兼容,JavaScript 引擎无法支持正则表达式后视运算符。这可以使用 .split('?')[0] 轻松修复

