【问题标题】:Meteor : Iron-Router error request entity too largeMeteor:Iron-Router 错误请求实体太大
【发布时间】:2018-12-14 01:21:38
【问题描述】:

我正在尝试将数据从节点 js 服务器发送到流星应用程序。在节点 js 应用程序中,我正在这样做:

axios.put('http://localhost:3000/api/project/'+id,{"data" :data, "idExtractor":idExtractor, "version":getVersion()})

其中 data 是我们的服务器给出的 XML 字符串

在我的流星应用程序中,我在 lib/Router.js 文件中的服务器路由上接收到这个,但是当数据太大时,流星服务器给我错误“套接字挂断”和“请求实体太大”。

我在 github 上尝试了 Iron Router 问题的解决方案,在 Router.js 文件中执行以下代码。

if (Meteor.isServer) {
  Router.onBeforeAction(Iron.Router.bodyParser.raw({ 
    type: '/', 
    only: ['creditReferral'], 
    verify: function(req, res, body){
      req.rawBody = body.toString(); 
    },
    where: 'server'
  }));
  Router.onBeforeAction(Iron.Router.bodyParser.urlencoded({
    extended: true,
    limit : '10mb'
  }));
  Router.onBeforeAction(Iron.Router.bodyParser.urlencoded({
    extended: true,
    limit : '10mb' 
  }), 
  { where: 'server'});
}

我尝试将它放在所有路由定义之前,在服务器路由之前,在所有路由之后,我也尝试将它放在启动块中的 server/main.js 中。

我也尝试用这个来改变节点服务器中的这些限制

express.urlencoded({
  limit: '10mb', 
  extended: true, 
  type: "application/x-www-form-urlencoded"
})
express.json({
  limit: '10mb', 
  strict: false, 
  type: "application/json"
})

还有这个

app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit :'10mb' }));

但我总是遇到同样的问题,非常感谢所有帮助。

【问题讨论】:

    标签: node.js express meteor websocket iron-router


    【解决方案1】:

    在尝试了许多解决方案后,我在 Router.js 中解决了这个问题:

    if (Meteor.isServer) {
        Router.onBeforeAction( Iron.Router.bodyParser.json({
            limit: '50mb'
        }), {
            except: ['creditReferral'],
            where: 'server'
        });
        Router.onBeforeAction( Iron.Router.bodyParser.raw({
            type: '*/*',
            only: ['creditReferral'],
            verify: function(req, res, body){
                req.rawBody = body.toString();
            },
            where: 'server'}));
    }
    

    这修复了entity too large错误,但是因为这个请求中的数据是xml,我不能直接解析成json,所以需要改一下

    var json = JSON.parse(this.request.body) 
    

    到以下:

    var stringify = JSON.stringify(this.request.body);
    var json = JSON.parse(stringify);
    

    它奏效了。希望这可以帮助遇到同样问题的人。

    【讨论】:

      猜你喜欢
      • 2013-11-23
      • 2016-08-05
      • 2016-02-10
      • 2016-02-01
      • 1970-01-01
      • 2019-03-17
      • 2016-07-16
      • 2017-03-05
      • 1970-01-01
      相关资源
      最近更新 更多