在node + express中使用文件上传模块multer

时间:2015-07-30 14:43:02

标签: node.js express multer

我正在尝试在基于Express 4的应用程序中实现文件上传。我遵循本教程:https://codeforgeek.com/2014/11/file-uploads-using-node-js/

在那段代码中:

var express = require('express');
var multer = require('multer');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

app.use(multer({
    dest: './uploads/',
    rename: function (fieldname, filename) {
        return filename + Date.now();
    },
    onFileUploadStart: function (file) {
        console.log(file.originalname + ' is starting ...')
    },
    onFileUploadComplete: function (file) {
        console.log(file.fieldname + ' uploaded to  ' + file.path)
        done = true;
    }
}));

行app.use(multer({

导致错误:

C:\Development\HERA\hera_node\node_modules\express\lib\application.js:206
    throw new TypeError('app.use() requires middleware functions');
          ^
TypeError: app.use() requires middleware functions
    at EventEmitter.use (C:\Development\HERA\hera_node\node_modules\express\lib\
application.js:206:11)
    at Object.<anonymous> (C:\Development\HERA\hera_node\app.js:17:5)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

可能是什么原因?我是node&amp;的新手。表达。解释requires middleware functions并没有告诉我任何事情。

2 个答案:

答案 0 :(得分:2)

本教程使用较旧版本的Multer(准确地说是v0.1.6),其中当前版本的Multer是1.0.1。在这些版本之间需要设置和使用Multer的方式发生了变化,这就是为什么教程代码不再适用于最新的Multer。

快速解决方法是安装旧版本:

open(LOGFILE,$logfile) or die "error";
for(;;){
      if($logfile=<LOGFILE>){
          ......................
    }
}

但是,在某些时候你可能想要转移到最新版本的Multer。

答案 1 :(得分:0)

//运行java-script文件并打开视图页面,然后上传文件并提交//

  • js file

const express = require('express');
const fileUpload = require('express-fileupload');
const app = express();
let date = require('date-and-time');

// default options
app.use(fileUpload());

app.post('/upload', function(req, res) {
  if (!req.files)
    return res.status(400).send('No files were uploaded.');

  // The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
  let sampleFile = req.files.sampleFile;
  let now = new Date();
  var now1 = date.format(now, 'YYYY_MM_DD_HH_mm_ss');
  var desired = now1.replace(/[^\w\s]/gi, '')
  var name = now1 + "_" + req.files.sampleFile.name;
  // Use the mv() method to place the file somewhere on your server
  sampleFile.mv("./../nodejs_image_upload_example/Images/" + name, function(err) {
    if (err)
      return res.status(500).send(err);

    res.send('File uploaded!');
  });
});


app.listen(2000, function(a) {
  console.log("Listening to port 2000");
});
<html>

<body>
  <form ref='uploadForm' id='uploadForm' action='http://localhost:2000/upload' method='post' encType="multipart/form-data">
    <input type="file" name="sampleFile" />
    <input type='submit' value='Upload!' />
  </form>
</body>

</html>

`