javascript 带有 app.use 的 multer 配置返回 TypeError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31495499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:54:19  来源:igfitidea点击:

multer configuration with app.use returns TypeError

javascriptnode.jsexpressmulter

提问by prcbass

I'm trying to configure multer in my app.js file (using node.js/express) in order to allow users to upload images. I have the following code in app.js:

我正在尝试在我的 app.js 文件(使用 node.js/express)中配置 multer 以允许用户上传图像。我在 app.js 中有以下代码:

//various require statements for passport, cookie parser, etc..

var multer = require('multer');

var app = express();

app.use(multer({dest:'./uploads/'}));

When I try to run the app I get TypeError: app.use() requires middleware functions

当我尝试运行该应用程序时,我得到 TypeError: app.use() requires middleware functions

I understand that this questions may require some more context with regards to my app so please let me know if additional information is needed.

我知道这个问题可能需要更多关于我的应用程序的上下文,所以如果需要更多信息,请告诉我。

Thank you

谢谢

EDIT: More code from app.js:

编辑:来自 app.js 的更多代码:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var session = require('express-session');
//var fs = require('fs');
var multer = require('multer');

//Mongo Database
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/test-api');

//Instagram-API
var api = require('instagram-node').instagram();

//Cookie Manager
var cookieParser = require('cookie-parser');

//Grid
//var Grid = require('gridfs-stream');

//Passport
var passport = require('passport');
var InstagramStrategy = require('passport-instagram').Strategy;

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

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(multer({dest:'./uploads/'}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(methodOverride());
app.use(session({secret: 'keyboard cat', resave: false, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));


// Make our db accessible to our router
app.use(function(req,res,next){
    req.db = db;
    next();
});

app.use('/', routes);

回答by stdob--

Problem is that multer({dest:'./uploads/'})return object, not middleware function. And if we look at the code module that we will see that there are three middlware functions:

问题是multer({dest:'./uploads/'})返回对象,而不是中间件函数。如果我们查看代码模块,我们会看到有三个中间件功能:

multer({dest:'./uploads/'}).single(...)
multer({dest:'./uploads/'}).array(...)
multer({dest:'./uploads/'}).fields(...)

Try one of them.

尝试其中之一。

回答by prcbass

Credit goes to @stdob-- for guiding me in the right direction. The multer documentation I was looking at seems to have been updated while I was working and therefore I was using the code incorrectly.

归功于@stdob--指导我朝着正确的方向前进。我正在查看的 multer 文档似乎在我工作时已更新,因此我错误地使用了代码。

Multer should be used with its middleware functions. Consequently the correct way to use this package is as follows:

Multer 应该与其中间件功能一起使用。因此,使用此包的正确方法如下:

var multer = require('multer');

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

var cpUpload = upload.single('postImg');

router.post('/createpost', cpUpload, function(req,res){

  console.log(req.file);
  res.redirect('/');

}

This code snippet will create a post route for '/createpost' and will upload a single document (by the form name 'postImg') to /uploads in the server. req.filecontains the JSON with the file information (such as the file path). Keep in mind that this will not automatically get rid of the uploaded file.

此代码片段将为“/createpost”创建一个发布路由,并将单个文档(通过表单名称“postImg”)上传到服务器中的 /uploads。req.file包含带有文件信息(例如文件路径)的 JSON。请记住,这不会自动删除上传的文件。

More information can be found here: https://www.npmjs.com/package/multer

更多信息可以在这里找到:https: //www.npmjs.com/package/multer

回答by Hamid Ali

this will do

这会做

app.use(multer({dest:'./public/images/uploads'}).any());