Javascript 在node.js中上载文件

Javascript 在node.js中上载文件,javascript,node.js,file-upload,busboy,Javascript,Node.js,File Upload,Busboy,我在node.js中上载文件时遇到问题。我不使用express。大多数在线示例都是使用expres上传文件。。 如果有任何指点,我将不胜感激 function fileUploader2(req, res, next) { var busboy = new Busboy({ headers: req.headers }); req.pipe(busboy); busboy.on('file', function(fieldname, file, filename, encoding, mime

我在node.js中上载文件时遇到问题。我不使用express。大多数在线示例都是使用expres上传文件。。 如果有任何指点,我将不胜感激

function fileUploader2(req, res, next) {
var busboy = new Busboy({ headers: req.headers });
req.pipe(busboy);

busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
  //  var saveTo = path.join(os.tmpDir(), path.basename(fieldname));
    file.pipe(fs.createWriteStream('./upload/' + filename));
});
busboy.on('finish', function() {
    res.writeHead(200, { 'Connection': 'close' });
    res.end("That's all folks!");
});


res.end();
})


上述方法不起作用。再说一次,我没有使用express。非常感谢您的帮助。

这里,我给您举了个例子。这对我有用

const Busboy = require('busboy');
const http = require('http');
const fs = require('fs');

const server = http.createServer((req, res) => {
  var busboy = new Busboy({
    headers: req.headers
  });
  busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    saveTo = `${__dirname}/${filename}`;
    file.pipe(fs.createWriteStream(saveTo));
  });
  busboy.on('finish', function() {
    res.writeHead(200, {
      'Connection': 'close'
    });
    res.end("That's all folks!");
  });
  return req.pipe(busboy);
  res.writeHead(404);
  res.end();
});

server.listen(8000, function() {
  console.log('magic at 8000');
});

在这里,我给你举了个例子。这对我有用

const Busboy = require('busboy');
const http = require('http');
const fs = require('fs');

const server = http.createServer((req, res) => {
  var busboy = new Busboy({
    headers: req.headers
  });
  busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    saveTo = `${__dirname}/${filename}`;
    file.pipe(fs.createWriteStream(saveTo));
  });
  busboy.on('finish', function() {
    res.writeHead(200, {
      'Connection': 'close'
    });
    res.end("That's all folks!");
  });
  return req.pipe(busboy);
  res.writeHead(404);
  res.end();
});

server.listen(8000, function() {
  console.log('magic at 8000');
});

你需要更具体地说明什么不起作用。如果您不使用express,req、res和next从何而来?您需要更具体地说明哪些不起作用。如果您不使用express,下一个req、res来自哪里?