【问题标题】:Express handling URIError: Failed to decode param快速处理 URIError: 无法解码参数
【发布时间】:2016-03-21 07:24:54
【问题描述】:
var express = require('express');
var app = express();

app.get('*', function (req, res) {
    var host = req.get('Host');
    return res.redirect(['https://', host, req.url].join(''));
});

var server = app.listen(8080, function () {
  console.log('starting');
});

我有一个将 http 重定向到 https 的简单脚本。这工作正常,除非有格式错误的 url,例如:website.com/%c0%ae%c0%ae。它显示如下内容:

URIError: Failed to decode param '/%c0%ae%c0%ae'
   at decodeURIComponent (native)
   at decode_param (/...<PROJECT DIRECTORY>.../node_modules/express/lib/router/layer.js:167:12)
   at Layer.match (/.../node_modules/express/lib/router/layer.js:143:15)
   at matchLayer (/.../node_modules/express/lib/router/index.js:557:18)
   at next (/.../node_modules/express/lib/router/index.js:216:15)
   at expressInit (/.../node_modules/express/lib/middleware/init.js:33:5)
   at Layer.handle [as handle_request] (/.../node_modules/express/lib/router/layer.js:95:5)
   at trim_prefix (/.../node_modules/express/lib/router/index.js:312:13)
   at /.../node_modules/express/lib/router/index.js:280:7
   at Function.process_params (/.../node_modules/express/lib/router/index.js:330:12)

当用户可以随机看到我的项目文件在服务器中的位置时,这并不好。有什么办法可以处理这个错误?

【问题讨论】:

  • 返回 res.redirect('https://'+host+req.url);试试这个

标签: node.js express


【解决方案1】:

感谢@Oleg 的提示。但不知何故,您的解决方案并没有为我记录错误。这是我想出的:

var express = require('express');
var app = express();

app.use(function(req, res, next) {
    var err = null;
    try {
        decodeURIComponent(req.path)
    }
    catch(e) {
        err = e;
    }
    if (err){
        console.log(err, req.url);
        return res.redirect(['https://', req.get('Host'), '/404'].join(''));    
    }
    next();
});

app.get('*', function (req, res) {
    return res.redirect(['https://', req.get('Host'), req.url].join(''));
});

var server = app.listen(8080, function () {
    console.log('Starting');
});

【讨论】:

  • 一个事后看来很明显的提示,但可能会为其他人节省一些时间:如果您的 express 后端服务器代理在 webpack-dev-server 后面(在开发 React 应用程序时使用),请不要不要忘记将格式错误的 URL 请求直接发送到服务器。如果您使用普通的前端端口发出请求,webpack-dev-server 将改为处理(barf on)格式错误的 URL,并且它不会被代理到您的实际后端应用程序,因此您的自定义处理程序赢了'没有机会被调用。
【解决方案2】:

可能的解决方法:

var express = require('express');
var app = express();

app.get('*', function (req, res) {
    // redirect regular paths
    var host = req.get('Host');
    return res.redirect(['https://', host, req.url].join(''));
});

// your express error handler
app.use(function(err, req, res, next) {
    // in case of specific URIError
    if (err instanceof URIError) {
        err.message = 'Failed to decode param: ' + req.url;
        err.status = err.statusCode = 400;

        // .. your redirect here if still needed
        return res.redirect(['https://', req.get('Host'), req.url].join(''));
    } else {
        // ..
    }
    // ..
});

var server = app.listen(8080, function () {
    console.log('starting');
});

【讨论】:

  • @pewpewlasers 让它工作你必须把它放在你的路线之后
【解决方案3】:
var express = require('express');
var app = express();

// handles 400 error
app.use((err, req, res, next) => {
  if (!err) return next();
  return res.status(400).json({
    status: 400,
    error: 'OOps! Bad request',
  });
});

编辑: 代码 sn-p 应该作为最后一个路由,它检查是否存在被其他路由跳过的错误,显然存在,并发送默认响应。当您将 % 添加为 API 端点的最后一个字符时会发生此错误。

【讨论】:

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