如何修复“ app.use()需要中间件功能”

时间:2019-07-04 06:27:55

标签: javascript node.js apache express ubuntu

因此,根据底部的错误消息,问题出现在第11行,这是我为multer提供一些参数的位置。看起来应该没有问题,我最初认为multer的安装不正确,但是所有multer文件(我相信)都位于node_modules下。现在我对可能出现什么问题一无所知。

var express = require('express');
var app = express();
var fs = require('fs');

var multer = require('multer');
var bodyParser = require('body-parser');


app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(multer({ dest: '/tmp/'  }));

app.get('/index.html', function (req, res) {
   res.sendFile( __dirname + "/" + "index.html" );
})

app.post('/file_upload', function (req, res) {
    console.log(req.files.file.name);
   console.log(req.files.file.path);
   console.log(req.files.file.type);
   var file = __dirname + "/" + req.files.file.name;

   fs.readFile( req.files.file.path, function (err, data) {
      fs.writeFile(file, data, function (err) {
         if( err ) {
            console.log( err );
            } else {
               response = {
                  message:'File uploaded successfully',
                  filename:req.files.file.name
               };
            }

         console.log( response );
         res.end( JSON.stringify( response ) );
      });
   });    
})

var server = app.listen(8080, function () {
   var host = server.address().address
   var port = server.address().port

   console.log("Post app listening to://%s:%s", host, port)
})
TypeError: app.use() requires a middleware function
    at EventEmitter.use (/home/asgeir/nodejs/first_test_app/node_modules/express/lib/application.js:210:11)
    at Object.<anonymous> (/home/asgeir/nodejs/first_test_app/server.js:11:5)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:27:21)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
/home/asgeir/nodejs/first_test_app/node_modules/express/lib/application.js:210
    throw new TypeError('app.use() requires a middleware function')
    ^

2 个答案:

答案 0 :(得分:2)

尝试

using app.use(multer({dest: '/tmp/'}).any());

请参考此问题https://stackoverflow.com/a/36020740/2558788

答案 1 :(得分:1)

似乎您对multer有问题。

在阅读文档时,您没有正确实现集成。

查看以下文档摘录:https://github.com/expressjs/multer#usage

您必须自定义需要的资源...

var express = require('express')
var multer  = require('multer')
var upload = multer({ dest: 'uploads/' })

var app = express()

app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
})

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
  // req.files is array of `photos` files
  // req.body will contain the text fields, if there were any
})

var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {
  // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
  //
  // e.g.
  //  req.files['avatar'][0] -> File
  //  req.files['gallery'] -> Array
  //
  // req.body will contain the text fields, if there were any
})