英文:
How to have a custom bodyLimit for a single api when using fastify with openapi
问题
我正在使用openapi和fastify模块从Node.js中使用fastify。我想要一个特定的路由具有非默认的请求体限制。这是否可能?我尝试在我的api.json定义中使用以下内容,但这不起作用。
"/api/myApi": {
"post": {
"x-fastify-config": {
"bodyLimit": 10000000
}
...
}
}
谢谢。
英文:
I am using fastify with openapi from nodejs with the modules
openapiGlue & fastify
I'd like one specific route to have a non-default body limit. Is this possible ? I tried the following in my api.json definition
"/api/myApi": {
"post": {
"x-fastify-config": {
"bodyLimit":10000000
}
...
but this does not work.
Thanks
答案1
得分: 1
无法从openapi JSON文件中找到方法来实现...但是...可以使用onRoute钩子来完成,该钩子在解析openapi JSON时运行。
fastify.addHook('onRoute', (routeOptions) => {
if (routeOptions.path === 'mypath') {
console.log(JSON.stringify(routeOptions));
routeOptions.bodyLimit = 10000000;
}
});
英文:
I could not find a way to do it from the openapi JSON file ... but ... it can be done with an onRoute hook which is run as openapi JSON is parsed.
fastify.addHook('onRoute', (routeOptions) => {
if (routeOptions.path === 'mypath') {
console.log(JSON.stringify(routeOptions));
routeOptions.bodyLimit=10000000
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论