0

我正在尝试在基于 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

可能是什么原因?我在节点和快递方面很新。解释requires middleware functions并没有告诉我任何事情。

4

2 回答 2

2

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

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

$ npm install multer@0.1.6

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

于 2015-07-31T12:25:12.920 回答
0

//运行java-script文件并打开查看页面然后上传文件并提交//

  • js文件

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>

`

于 2018-02-28T07:54:18.793 回答