简体   繁体   中英

Node.js & request module : Start upload from a readable stream

When a user upload a file on my node.js server, I need to upload that same file to another server.

I was wondering if I could start sending the uploaded parts to the second server without having to wait the file has been completly uploaded on my node.js server.

I'm using the request module https://github.com/mikeal/request to upload to the second server.

The code below waits until the user has finished his upload before starting the second upload (Though I'm not 100% sure about that) :

app.post('/upload', function(req, res, next){
    fs.readFile(req.files.file.path, function (err, data) {
        var newName = moment().format('YYYYMMDDHHmmss') + "_" + (Math.floor(Math.random() * (10000 - 0) + 0));
        var name = newName + "." + req.files.file.extension;
        var newPath = "public/uploads/"+name;
        fs.writeFile(newPath, data, function (err) {
            if (err) {
                throw err;
                res.send("error");
            }
            fs.unlink(req.files.file.path, function (err) {
                if (err) response.errors.push("Erorr : " + err);
                console.log('successfully deleted temp file : '+ req.files.file.path );
            });
            var uploadurl = "http://second.server.com/upload;
            var r = request.post(uploadurl, function optionalCallback (err, httpResponse, body) {
                if (err) {
                    return console.error('upload failed:', err);
                }
                console.log('Upload successful!  Server responded with:', body);
            });
            var form = r.form();
            form.append('file', fs.createReadStream(newPath));           
            res.send(newPath);
        });
    });
});

Here's how you might do it with busboy (note: this requires that your current body parsing middleware does not run for this particular route otherwise the request data will already have been consumed):

var Busboy = require('busboy');

// ...

app.post('/upload', function(req, res, next) {
  var busboy = new Busboy({ headers: req.headers }),
      foundFile = false,
      uploadurl = 'http://second.server.com/upload',
      form,
      r;

  busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    if (foundFile || fieldname !== 'file')
      return file.resume(); // skip files we're not working with

    foundFile = true;

    r = request.post(uploadurl, function(err, httpResponse, body) {
      if (err)
        return console.error('upload failed:', err);
      console.log('Upload successful!  Server responded with:', body);
    });

    form = r.form();
    form.append('file', file);
  }).on('finish', function() {
    res.send('File ' + (foundFile ? '' : 'not ') + 'transferred');
  });

  req.pipe(busboy);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM