Express URIError:无法解码参数

2024-05-21

当请求的参数包含时,我将 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 the ^^被编码并且 url 变成http://localhost:3000/summoner/eune/%5E%5Efaker一切都很完美。我可以通过遵循这个答案来修复这个错误快速处理 URIError:无法解码参数 https://stackoverflow.com/questions/36125216/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/%%faker to http://localhost:3000/summoner/eune/%25%25faker如果我使用return app.render(req, res, "/_error");它将把 next.js 提供的默认错误页面发送回用户,但这不是我想要的。我想处理% like ^.

所以我的问题是:

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

我使用的是节点 v8.9.1,express ^4.16.3。 请详细回答我是一名初学者开发人员。 谢谢你的时间。


正如您所指出的,网址是百分比编码 https://en.wikipedia.org/wiki/Percent-encoding and http://localhost:3000/summoner/eune/%%faker只是作为 url 无效。

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

就你而言,%不会自动更改为%25因为浏览器不知道何时替换%以及何时离开它。例如:当您输入时%25%25faker,该 url 应该按原样使用还是应该替换为%2525%2525faker ?

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Express URIError:无法解码参数 的相关文章

随机推荐