我正在尝试创建一个使用fastify消耗multipart / form-data的api。我正在使用该fastify-multer
插件来尝试发送文件,以便可以将它们传递给另一个第三方api。现在,我只是想用Postman进行测试,但是当我用png / pdf文件发出发布请求时,request.files
则是未定义的。我已经尝试对fastify-multer
文档进行了以下修改,并对其进行了修改。我将路由保存在一个单独的目录中,然后fastify-autoload
用来将它们引入。server.js
正在注册我的所有插件,并且我有一个index.js
启动服务器(并且工作正常,因此未在下面包括在内)。这是我的设置:
server.js
const path = require('path')
const fp = require('fastify-plugin')
const autoload = require('fastify-autoload')
const multer = require('fastify-multer')
const upload = multer({ storage: multer.memoryStorage() })
async function registerPlugins(server, config) {
server
.register(multer.contentParser)
.register(require('fastify-cors'), config.cors)
.register(require('fastify-helmet'))
.register(require('fastify-sensible'))
// auto-register all plugins
.register(autoload, {
dir: path.join(__dirname, 'plugins'),
ignorePattern: /.*test.js/,
options: config
})
// auto-register all routes
.register(autoload, {
dir: path.join(__dirname, 'routes'),
ignorePattern: /.*test.js/,
options: config
})
server.decorate('upload', upload)
}
module.exports = fp(registerPlugins)
my.routes.js
const fp = require('fastify-plugin')
const { createMerchantSchema } = require('./schema')
async function myRoutes(server, options) {
server.post(
'/merchant/:merchantId/uploadAttachment',
{ preHandler: server.upload.array('files', 8) },
async (request, reply) => {
console.log('FILES:')
console.log(request.files)
return reply.code(200).send('SUCCESS')
}
)
}
module.exports = fp(myRoutes)
我不会为您显示目录结构带来麻烦,但是我还有其他6条路由(此处未显示)可以正常工作,并且服务器启动时没有错误。这是我发送时的控制台输出POST /merchant/12345/uploadAttachment
:
[nodemon] starting `node index.js`
[1597110502609] INFO (96323 on 7L6BHR2.agrium.com): Server listening at http://0.0.0.0:3333
[1597110502609] INFO (96323 on 7L6BHR2.agrium.com): Server running at: http://0.0.0.0:3333
[1597110510972] INFO (96323 on 7L6BHR2.agrium.com): incoming request
reqId: 1
req: {
"method": "POST",
"url": "/merchant/123456/uploadAttachment",
"hostname": "localhost:3333",
"remoteAddress": "127.0.0.1",
"remotePort": 51207
}
FILES:
undefined
[1597110510981] INFO (96323 on 7L6BHR2.agrium.com): request completed
reqId: 1
res: {
"statusCode": 200
}
responseTime: 8.47090208530426
我不是Content-Type: multipart/form-data
自己发送标题的。邮递员足够聪明,可以添加它,并且不提供它就意味着我不必越过自己的边界。
我真的不确定这里出了什么问题。有没有人这样设置fastify-multer
?我有什么想念的吗?我正在拔头发!任何建议将不胜感激。提前致谢!!!
Thanks to the creator of fastify-multer
, Maksim Sinik, for helping me solve this silly issue. All credit goes to him. I found out that, in fact, fastify-multer
works great with fastify-autoload
and fastify-plugin
. My issue was that in Postman, I wasn't allowing the auto-generated header content-length
to be sent with the request:
Internally, fastify-multer
uses npm module type-is to help discover the type of the request. If content-length
is not sent, content-type
is not sent as multipart/form-data
and the type-is
hasBody
method returns null, which causes request.files
to be undefined.
Moral of the story: if you want to test fastify-multer
(or any other multipart data processor for that matter) with Postman, you must ensure the auto-generated content-length
AND content-type
header are enabled (selected):
我真的希望这对以后的人有所帮助。我正在拔头发!再次感谢马克西姆。您可以找到他在Github上所做的更多工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句