我正在尝试将数据从 Node js服务器发送到 meteor 应用程序。在 Node js 应用程序中,我正在这样做:
axios.put('http://localhost:3000/api/project/'+id,{"data" :data, "idExtractor":idExtractor, "version":getVersion()})
其中 data 是我们服务器给出的 XML 字符串
在我的 meteor 应用程序中,我在 lib/Router.js 文件中的服务器路由上接收到此信息,但是当数据太大时, meteor 服务器会给我错误“套接字挂起”和“也请求实体”大”。
我尝试了 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'});
}
我尝试将它放在所有路由定义之前、服务器路由之前、所有路由之后,我还尝试将它放在启动 block 中的 server/main.js 中。
我还尝试用此更改 Node 服务器中的这些限制
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' }));
但我总是遇到同样的问题,我们将不胜感激所有帮助。
最佳答案
尝试了很多解决方案后,我在 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'}));
}
这修复了实体太大的错误,但是因为这个请求中的数据是xml,我无法直接将其解析为json,所以我需要更改我的
var json = JSON.parse(this.request.body)
以下内容:
var stringify = JSON.stringify(this.request.body);
var json = JSON.parse(stringify);
并且成功了。希望这可以帮助遇到同样问题的人。
关于node.js - meteor : Iron-Router error request entity too large,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51193351/