使用editor.swagger生成Nodejs代码,在体内我想发送10mb但无法发送,我收到此错误:
Error: request entity too large
at makeError (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\body-parser\node_modules\raw-body\index.js:154:15)
at readStream (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\body-parser\node_modules\raw-body\index.js:188:15)
at getRawBody (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\body-parser\node_modules\raw-body\index.js:95:12)
at read (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\body-parser\lib\read.js:64:3)
at jsonParser (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\body-parser\lib\types\json.js:116:5)
at c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\middleware\swagger-metadata.js:72:9
at urlencodedParser (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\body-parser\lib\types\urlencoded.js:91:37)
at bodyParser (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\middleware\swagger-metadata.js:68:5)
at c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\middleware\swagger-metadata.js:258:5
at c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\async\lib\async.js:356:13
我已经尝试过这个选项,但仍然是同样的错误。以下是index.js的示例代码:
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
/*app.use(middleware.swaggerMetadata(
bodyParser.json({limit: "50mb"}),
bodyParser.urlencoded({ limit: "50mb", extended: true })
)
);*/
// Validate Swagger requests
app.use(middleware.swaggerValidator());
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options));
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi());
//app.use(bodyParser.json({limit: "50mb"}));
//app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
app.use(bodyParser.json({limit: 10 * 1024 * 1024}));
app.use(bodyParser.raw({limit: 10 * 1024 * 1024}));
app.use(bodyParser.text({limit: 10 * 1024 * 1024}));
app.use(bodyParser.urlencoded({limit: 10 * 1024 * 1024, extended: true}));
答案 0 :(得分:4)
根据https://github.com/apigee-127/swagger-tools/issues/248中指定的说明,您希望在初始化swagger之前配置bodyParser
,如下所示:
app.use(bodyParser.json({limit: 10 * 1024 * 1024}));
app.use(bodyParser.raw({limit: 10 * 1024 * 1024}));
app.use(require("./router")); // in this sample, this is where you configure your Swagger middleware
这样,Swagger将使用您配置的bodyParser
配置限制。我正在解决同样的问题,这解决了它。
答案 1 :(得分:0)
这是解码请求正文的body-parser错误。它有一个尺寸限制选项:
https://github.com/expressjs/body-parser#limit
swagger-tools使用它而不修改请求大小限制,因此默认情况下限制为100kb:
https://github.com/apigee-127/swagger-tools/blob/master/middleware/swagger-metadata.js#L48
添加已配置的正文解析器不会影响swagger-tools的内部调用。这个问题应该通过swagger工具来解决。否则你只能破解上面的代码。