本文介绍了带有express的node.js如何从url中删除查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,该按钮执行到我的页面的操作并向查询字符串添加过滤器.我的代码将该过滤器应用于网格...但是用户可以删除/编辑该过滤器.由于他们可以看到在网格中应用了什么过滤器,因此我想在显示页面时从查询字符串中删除?filter = blah. (如果在页面上并且URL上显示?filter = columnA最初是正确的,则可能会造成混淆,但是用户删除了该过滤器并在columnB上应用了一个新的过滤器....但是查询字符串仍然显示?filter-columnA.网格可以处理更改的过滤器,而无需回发.)我该怎么做?而且,如果您无法删除/更新查询字符串,是否可以解析它,然后仅在没有查询字符串的情况下将其重定向到主页?将过滤器保存到var过滤器后,查询字符串中将不再需要它.

这是显示页面的代码

 >

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
            }
    });

};
解决方案

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.

这篇关于带有express的node.js如何从url中删除查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:03