fastify-multer,使用Postman测试,request.files未定义

问题描述

我正在尝试创建一个使用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,'routes'),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

邮递员似乎除了头数据外什么也没有发送。该请求仅为408b:

Postman POST call

我不是自己发送Content-Type: multipart/form-data标头。邮递员足够聪明,可以添加它,并且不提供它就意味着我不必越过自己的边界。

我真的不确定这里出了什么问题。是否有人这样设置fastify-multer我有什么想念的吗?我正在拔头发!任何建议将不胜感激。提前谢谢!!!

解决方法

感谢fastify-multer的创建者Maksim Sinik帮助我解决了这个愚蠢的问题。所有的荣誉归他所有。我发现,实际上fastify-multerfastify-autoloadfastify-plugin一起使用效果很好。我的问题是在Postman中,我不允许将自动生成的标头content-length与请求一起发送:

image of content-length header disabled

在内部,fastify-multer使用npm模块type-is来帮助发现请求的类型。如果未发送content-length,则不会以content-type的形式发送multipart/form-data,并且type-is hasBody方法返回null,这将导致request.files不确定

故事的寓意:如果您想使用Postman测试fastify-multer(或与此相关的任何其他多部分数据处理器),则必须确保自动生成的content-lengthcontent-type标头已启用(选中):

image of content-type and content-length headers enabled

我真的希望这对以后的人有所帮助。我正在拔头发!再次感谢马克西姆。您可以在Github上找到他所做的更多工作。