-1

我在 NodeJS 中有一个 API,我可以在其中上传文件,并使用connect-busboy包在服务器端接收它。

因此,例如,这是处理请求的代码的一部分:

var app = require('express')();
var busboy = require('connect-busboy');

app.use(busboy({
        highWaterMark: 2 * 1024 * 1024,
        limits: {
            fileSize: 1024 * 1024 * 1024 // 1 GB
        },
        immediate: true
    }));

var busboyHandler = function (req, res) {
    if (req.busboy) {
        req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
            console.log('received file ', req.path, fieldname, file, filename, encoding, mimetype);
        });

        req.busboy.on('field', function(key, value, keyTruncated, valueTruncated) {
            console.log('field..', key, value, keyTruncated, valueTruncated);
        });

        req.busboy.on('finish', function() {
            console.log('busboy finished');
        });
    }
};

app.post('api/documents/files', busboyHandler); 

当我启动 APInpm start并将文件直接上传到此 API 时,这很有效,但是,当我配置 Nginx Docker 时,它适用于非常小的文件,但对于大多数文件,它们不会成功上传。

nginx.conf这是我的文件的摘录:

user nobody nogroup;
worker_processes auto;          # auto-detect number of logical CPU cores

events {
  worker_connections 512;       # set the max number of simultaneous connections (per worker process)
}

http {
  include mime.types;

  client_max_body_size 100M;
  client_body_buffer_size 256k;

  upstream api_doc {
    server 192.168.2.16:4956;
  }

  server {
    listen *:4000;                # Listen for incoming connections from any interface on port 80
    server_name localhost;             # Don't worry if "Host" HTTP Header is empty or not set
    root /usr/share/nginx/html; # serve static files from here

    client_max_body_size 100M;
    client_body_buffer_size 256k;

    location /api/documents {
        proxy_pass http://api_doc;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
     }
  }

}

我看到了received file日志,但是在Nginx下,从来没有busboy finished日志,不像在没有Nginx的情况下直接调用API。

我尝试更改这些 Nginx 配置,但没有成功:client_max_body_size, client_body_buffer_size. 在我看来,API 只接收较大文件的文件块,而不是应有的整个文件或所有块。

任何帮助,将不胜感激。

谢谢,西蒙

4

1 回答 1

0

原来问题出在其他地方,我在文件完全上传之前就开始读取传入流,因此由于某些原因导致传入流中断。

于 2017-01-12T12:44:13.347 回答