【问题标题】:Express URIError: Failed to decode paramExpress URIError:无法解码参数
【发布时间】:2018-09-09 19:27:01
【问题描述】:

当请求的参数包含 % 时,我将 next.js 与自定义快速服务器一起使用,这会导致此错误:

URIError: Failed to decode param '%%faker'
at decodeURIComponent (<anonymous>)
at decode_param (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\layer.js:172:12)
at Layer.match (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\layer.js:148:15)
at matchLayer (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:574:18)
at next (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:220:15)
at middleware (D:\ahmed\coding\react js\with-redux-app\node_modules\http-proxy-middleware\lib\index.js:43:7)
at Layer.handle [as handle_request] (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:317:13)
at D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:335:12)
at next (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:275:10)
at expressInit (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\middleware\init.js:40:5)
at Layer.handle [as handle_request] (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:317:13)
at D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:335:12)

例如,如果请求是http://localhost:3000/summoner/eune/%%faker,则会发生错误,但如果它是http://localhost:3000/summoner/eune/^^faker,则^^ 被编码并且url 变为http://localhost:3000/summoner/eune/%5E%5Efaker,并且一切正常。我可以通过遵循此答案@987654321 来修复此错误@像这样:

server.use((err, req, res, next) => {
  if (err instanceof URIError) {
    err.message = "Failed to decode param: " + req.url;
    err.status = err.statusCode = 400;
    console.log(err);
    return res.redirect(`http://${req.get("Host")}${req.url}`);
    // return app.render(req, res, "/_error");
  }
});

return res.redirect(`http://${req.get("Host")}${req.url}`); 这会将用户从http://localhost:3000/summoner/eune/%%faker 重定向到http://localhost:3000/summoner/eune/%25%25faker,如果我使用return app.render(req, res, "/_error"); 它会将next.js 提供的默认错误页面发送回用户,但这不是我想要的。我想像^ 一样处理%

所以我的问题是:

  1. 为什么 % 没有被编码为 %25 以及是否有办法实现它?
  2. 浏览器或快递谁负责编码?
  3. 处理此错误的最佳方法是什么?

我正在使用节点 v8.9.1,表示 ^4.16.3。 请详细回答我是初学者。 感谢您的宝贵时间。

【问题讨论】:

    标签: node.js express url-encoding next.js


    【解决方案1】:

    正如您所指出的,网址是 percent-encodedhttp://localhost:3000/summoner/eune/%%faker 只是作为网址无效。

    当您输入无效的 url 时,大多数浏览器都会将其更改为有效的内容,例如:http://localhost:3000/test test 会自动更改为 http://localhost:3000/test%20test,但这只是避免太多错误的后备。

    在您的情况下,% 不会自动更改为 %25,因为浏览器无法知道何时替换 % 以及何时离开它。例如:当你输入%25%25faker时,这个url是应该原样使用还是应该替换为%2525%2525faker

    总而言之:您必须在任何时候使用有效的 url,而不是依赖浏览器的好意。

    【讨论】:

      猜你喜欢
      • 2018-11-22
      • 2017-12-08
      • 1970-01-01
      • 2019-03-16
      • 2021-03-03
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      相关资源
      最近更新 更多