【问题标题】:How do I access the formData in this request via node?如何通过节点访问此请求中的 formData?
【发布时间】:2015-08-14 13:22:17
【问题描述】:

我正在使用此代码发出请求:

 Request {
  url: 'http://localhost:10002/asset/upload'
  method: 'POST'
  formData:
    filename: fileName
    code: imageCode
    type: 'raw'
    event_id: eventId
    file: fs.createReadStream(filePath)
    slug: "testing-the-stuff"

在我的快速应用程序中,我有这样一条路线:

  app.post "/asset/upload", (req, res, next) ->
    assetController.upload req, res, next  

还有一个看起来像这样的控制器:

exports.upload = (req, res, next) ->
  console.log req.body
  res.json 'upload'

一切都很好,没有失败,但身体总是空的?我不确定是否有其他方法可以访问"formData"

作为参考,我使用bodyparser

【问题讨论】:

    标签: node.js http express request body-parser


    【解决方案1】:

    经过一些研究,我发现 bodyparser 无法处理多部分表单数据。为了解决这个问题,我简单地将 bodyparser 替换为 busboy-body-parser

    busboyBodyParser = require('busboy-body-parser')
    app.use busboyBodyParser()
    

    这个库似乎结合了 bodyparsers 正常表单解析和 busboys 处理多部分表单数据和文件的能力。

    https://www.npmjs.com/package/busboy-body-parser

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-02
      • 2020-10-02
      • 2022-10-17
      • 2014-11-29
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      相关资源
      最近更新 更多