【问题标题】:Node.js Busboy not firing "file" event before "finish" eventNode.js Busboy 在“完成”事件之前没有触发“文件”事件
【发布时间】:2021-10-15 16:48:14
【问题描述】:

我只是想通过 Node.js/Next.js 将文件从浏览器上传到 S3,而 busboy 没有发出 file 事件。

这基本上是我的 FE 代码:

Upload.prototype.to = function(options, fn){
  // TODO: x-browser
  var path;
  if (typeof options == 'string') {
    path = options;
    options = {};
  } else {
    path = options.path;
  }
  var self = this;
  fn = fn || function(){};
  var req = this.req = new XMLHttpRequest;
  req.open('POST', path);
  req.onload = this.onload.bind(this);
  req.onerror = this.onerror.bind(this);
  req.upload.onprogress = this.onprogress.bind(this);
  req.onreadystatechange = function(){
    if (4 == req.readyState) {
      var type = req.status / 100 | 0;
      if (2 == type) return fn(null, req);
      var err = new Error(req.statusText + ': ' + req.response);
      err.status = req.status;
      fn(err);
    }
  };
  var key, headers = options.headers || {};
  for (key in headers) {
    req.setRequestHeader(key, headers[key]);
  }
  var body = new FormData;
  body.append(options.name || 'file', this.file);
  var data = options.data || {};
  for (key in data) {
    body.append(key, data[key]);
  }
  req.send(body);
};

所做的就是向/api/<path> 发出请求,并使用file 名称为我尝试上传的所选jpeg 进行。这是我在服务器请求正文中收到的内容:

body: '------WebKitFormBoundaryWbaXO8J6c8aI7Q4B\r\n' +
    'Content-Disposition: form-data; name="file"; filename="1-profile.jpg"\r\n' +
    'Content-Type: image/jpeg\r\n' +
    '\r\n' +
    '����\x00\x10JFIF...

标题包括这些:

  connection: 'keep-alive',
  'content-length': '41079',
  pragma: 'no-cache',
  'cache-control': 'no-cache',
  'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryWbaXO87Kc9aI2Q4B',
  accept: '*/*',
  origin: 'http://localhost:3000',

我的服务器代码如下所示:

import fetchId from '../../../../utils/get-next-id-from-pg'
import Busboy from 'busboy'
import s3 from 'initializers/s3'

export default async function(req, res) {
  if (req.method === 'POST') {
    const id = await fetchId('image')

    return new Promise((resolve, rej) => {
      const busboy = new Busboy({ headers: req.headers });
      busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
        console.log('Field [' + fieldname + ']: value: ' + inspect(val));
      })
      busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        console.log('params')
        const params = {
          Bucket: 'mybucket123',
          Key: id,
          Body: file
        }
        s3.upload(params, (err, data) => {
          console.log('s3', err, data)
          res.setHeader('Connection', 'close');
          res.status(200).json({ records: [ { id } ]})
          resolve()
        })
      })
      busboy.on('finish', function() {
        console.log('finish')
      })
      req.pipe(busboy);
    })
  } else {
    res.writeHead(405, { 'content-type': 'text/plain' });
    res.end("Method not allowed. Send a POST request.");
    return;
  }
}

它会记录finish,仅此而已,它不会记录filefield。我在这里想念什么?它不会在 on('file') 处理程序或任何 s3 内容中记录 params。我开始深入研究 busboy 源代码,但有没有更好的方法?我做错了什么,或者与 Next.js 一起使用的库是什么?

我正在使用 Next.js 和 dotenv,这会导致奇怪的问题吗?

【问题讨论】:

    标签: javascript node.js file-upload stream busboy


    【解决方案1】:

    万岁我figured it out,将其添加到 Next.js 路由中:

    export const config = {
      api: {
        bodyParser: false,
      },
    }
    

    https://nextjs.org/docs/api-routes/api-middlewares#custom-config

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 2012-11-27
      • 1970-01-01
      • 2013-08-15
      相关资源
      最近更新 更多