gpt4 book ai didi

node.js - Express URI错误: Failed to decode param

转载 作者:太空宇宙 更新时间:2023-11-04 00:07:02 29 4
gpt4 key购买 nike

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

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 以及所有内容工作完美。我可以通过遵循此答案 Express handling URIError: Failed to decode param 来修复此错误像这样:

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/%%fakerhttp://localhost:3000/summoner/eune/%25%25faker 如果我使用 return app.render(req, res, "/_error"); 它将把 next.js 提供的默认错误页面发送回用户,但这不是我想要的。我想像 ^ 一样处理 %

所以我的问题是:

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

我使用的是 Node v8.9.1,express ^4.16.3。请详细回答我是一名初学者开发人员。感谢您抽出时间。

最佳答案

正如您所指出的,网址是 percent-encoded并且 http://localhost:3000/summoner/eune/%%faker 作为 URL 无效。

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

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

总之:您必须在任何时间点使用有效的 URL,而不是依赖浏览器的善意。

关于node.js - Express URI错误: Failed to decode param,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51786690/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com