简体   繁体   English

修改req.url不会更新url参数(req.query)

[英]Modifying req.url doesn't update the url parameters (req.query)

Since Google Sitemap doesn't allow & in its XML . 由于Google Sitemap不允许&在其XML中使用 I've escaped the url (like ' /path?a=123&b=hello ') which contains two parameters now becomes /path?a=123%26b=hello and I thought browser will automatically unescape the url back. 我已经转义了包含两个参数的网址(例如' /path?a=123&b=hello ),现在变成了/path?a=123%26b=hello ,我认为浏览器会自动取消转义该网址。 But it's not. 但事实并非如此。 And Google bot is giving errors. 而且Google机器人发出了错误。

So I add a middleware function on top of the stack before calling the mentioned route. 因此,在调用上述路由之前,我在堆栈顶部添加了一个中间件函数。

app.use(function (req, res, next) {
        debug(req.url);
        req.url = unescape(req.url);
        debug(req.url);
        // This works as expected
        next();
});

But I when i perform req.query.a and req.query.b (Google bot is visiting using the escaped URL ie /path?a=123%26b=hello ), It's giving the original value ie req.query.a gives 123%26b=hello and req.query.b gives undefined and when I check req.url it is giving the unescaped url which is correct. 但是我执行req.query.areq.query.b (Google机器人使用转义的URL来访问时,即/path?a=123%26b=hello ),它给出的是原始值,即req.query.a gives 123%26b=helloreq.query.b gives undefined ,当我检查req.url ,给出的是未转义的url,这是正确的。

What do you suggest? 你有什么建议?

UPDATE: 更新:

One quick solution I could think of is (I haven't tried this but I think it will work) 我能想到的一个快速解决方案是(我没有尝试过,但我认为它会起作用)

// After unescaping req.url
// test.com/path?a=123&b=hello
var query = req.url.split('?')[1].split('&');
var a = query[0].split('=')[1];
var b = query[1].split('=')[1];

// It looks very dirty

In XML you should escape & as & 在XML中,您应转义&& :

/path?a=123&b=hello

%26 is used for the & sign within a value as you already found out. 您已经发现, %26用于值中的&符号。

Try HTML5 History API it will work : 试用HTML5 History API ,它将起作用:

History.pushState() History.pushState()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM