当请求的参数包含时,我将 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 ^
.
所以我的问题是:
- why the
%
没有被编码为%25
是否有办法实现?
- 谁负责浏览器或express的编码?
- 处理此错误的最佳方法是什么?
我使用的是节点 v8.9.1,express ^4.16.3。
请详细回答我是一名初学者开发人员。
谢谢你的时间。