【发布时间】: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 提供的默认错误页面发送回用户,但这不是我想要的。我想像^ 一样处理%。
所以我的问题是:
- 为什么
%没有被编码为%25以及是否有办法实现它? - 浏览器或快递谁负责编码?
- 处理此错误的最佳方法是什么?
我正在使用节点 v8.9.1,表示 ^4.16.3。 请详细回答我是初学者。 感谢您的宝贵时间。
【问题讨论】:
标签: node.js express url-encoding next.js