node.js 不能 app.use(multer)。“需要中间件功能”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31496100/
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
Cannot app.use(multer). "requires middleware function" error
提问by Jo?o Menighin
I'm just starting learning NodeJS and I am stuck with a problem. I would like to upload files to my server. To do so I searched and found out this module multer. Doing as the example on GitHub works:
我刚刚开始学习 NodeJS,但遇到了一个问题。我想将文件上传到我的服务器。为此,我搜索并找到了这个模块multer。按照 GitHub 上的示例进行操作:
var express = require('express');
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
var app = express()
app.post('/uploadImage', upload.single('image'), function(req, res) {
console.log(req.file);
});
On posting an image with FormData to /uploadImagethe image is saved in the uploads/directory. The thing is the image is saved with a strange name and I would like to save it with its original name.
To do so I understood that I would have to call app.use(multer({ dest: 'uploads/' }))'and then I would be able to access req.filein my function like:
在将带有 FormData/uploadImage的图像发布到图像时,该图像保存在uploads/目录中。问题是图像用一个奇怪的名字保存,我想用它的原始名称保存它。为此,我知道我必须调用app.use(multer({ dest: 'uploads/' }))',然后才能req.file在我的函数中访问,例如:
app.post('/uploadImage', function(req, res) {
console.log(req.file);
});
But I get an error on trying app.use():
但是我在尝试 app.use() 时遇到错误:
TypeError: app.use() requires middleware functions
at EventEmitter.use (project\node_modules\express\lib\application
.js:209:11)
Im using NodeJS 0.12.7 and Express 4.13.1
我使用 NodeJS 0.12.7 和 Express 4.13.1
How can I achieve that upload? Thanks.
我怎样才能实现上传?谢谢。
回答by Rubén Marrero
You need to use app.use(multer({dest:'./uploads/'}))in the form of one of these:
您需要以app.use(multer({dest:'./uploads/'}))下列形式之一使用:
app.use(multer({dest:'./uploads/'}).single(...));
app.use(multer({dest:'./uploads/'}).array(...));
app.use(multer({dest:'./uploads/'}).fields(...));
ie:
IE:
app.use(multer({dest:'./uploads/'}).single('photo'));
And be sure to have something like:
并确保有类似的东西:
<form action="/postPhotos" enctype="multipart/form-data" method="post">
<input type="file" name="photo">
<input type="submit" value="Upload photo">
</form>
In your html.
在你的 html 中。
回答by Homar Roman Franco
var app = require('express');
var multer = require('multer');
app=express();
app.use(multer({dest:__dirname+'/file/uploads/'}).any());
app.post('/upload',function(req,res){
console.log(req.files);
res.redirect('/');
});
回答by Salvatorelab
The answer from @127.0.0.1 is correct, but in case you are using Express Router, the code changes a little bit:
@127.0.0.1 的答案是正确的,但如果您使用的是Express Router,则代码会稍作更改:
var express = require('express');
var multer = require('multer');
var router = express.Router();
var uploads = multer({
dest: 'public/uploads/'
});
router.post('/upload', uploads.single('avatar'), function(req, res, next) {
console.log(req.file);
//...
});
And the important bit, the form encodingshould be enctype="multipart/form-data"as already said, like that:
重要的一点,表单编码应该enctype="multipart/form-data"像已经说过的那样:
<form action="/upload" enctype="multipart/form-data" method="post">
<input type="file" name="avatar">
<input type="submit" value="Go avatar go!">
</form>
回答by Jacek Wojcik
Since version 1.0.0
从版本 1.0.0
var upload = multer({ dest: 'tmp/' });
app.post('/file_upload', upload.single('photo'), function (req, res) {
回答by Aditya
You can't change the file name using multer but you can use Node.js stream and fs module to copy the content of already uploaded file to new file(set as original file name) on same folder and delete old one.
您不能使用 multer 更改文件名,但您可以使用 Node.js stream 和 fs 模块将已上传文件的内容复制到同一文件夹中的新文件(设置为原始文件名)并删除旧文件。
First of all import fs, path and multer in your node script.
首先在你的节点脚本中导入 fs、path 和 multer。
var express = require('express');
var multer = require('multer');
var fs = require('fs');
var pathModule = require('path');
Now, set destination directory of any type of files using multer as below.
现在,使用 multer 设置任何类型文件的目标目录,如下所示。
var app = express();
app.use(multer({dest:__dirname+'/resoucres/'}).any());
Now use stream and fs to resolve your issue.
现在使用流和 fs 来解决您的问题。
app.post('/uploadImage', function(request, response) {
var readerStream = fs.createReadStream(request.files[0].path);
var dest_file = pathModule.join(request.files[0].destination, request.files[0].originalname);
var writerStream = fs.createWriteStream(dest_file);
var stream = readerStream.pipe(writerStream);
stream.on('finish', function(){
fs.unlink(request.files[0].path);
});
});
回答by Lekan Stephen
Changing to app.use(multer({dest:'./uploads/'}).single('photo')); in the app.js works for me to start the server
更改为 app.use(multer({dest:'./uploads/'}).single('photo')); 在 app.js 中,我可以启动服务器
回答by Haifeng Fu
I encountered the same problem. And I solved it. For the first problem, how to get the original file name? I printed the whole request and found we could get original name by using the path "req.file.originalname"
我遇到了同样的问题。我解决了它。第一个问题,如何获取原始文件名?我打印了整个请求,发现我们可以通过使用路径“req.file.originalname”获得原始名称
and the other question, how to use "app.use()"?
refers: here
另一个问题,如何使用“app.use()”?
指:这里

