A lot of people will follow the node. js actual combat that book together, yes, in general, we will be smooth, but sometimes very depressed, such as Multer.
The author uses 0.1.6, this is already too old, now is the 1.1.0, of course you can choose the old version, but as our programmer, how can stay.
First the error will be
New TypeError ('app.use () requires middleware functions');
This does not matter, we Baidu, this has a reference: http://segmentfault.com/q/1010000003050818?sort=created
I now upgrade Multer 1.1.0, also encountered the same problem, and then go to his official website to understand.
I can't write that anymore.
app.use(multer({dest:‘./‘}));
You're going to write in the route.
var multer =Require Multer '); var upload = Multer ({dest: '/upload ', Upload.fields ([{name: ' file1 '}, {name: ' File3 '}, {name: File4 '}, {name: ' File5 '}]), Function (req, res, next) {for (var I in req.files) {console.log (Req.files[i]);} req.flash ( Span class= "hljs-string" > ' success ', ' file upload succeeded! '); Res.redirect ( '/upload ');});
/span>
Yes, that's what we wrote in Index.js.
varMulter = require ('Multer');varUpload = Multer ({dest:'./public/images/user'}); App.post ('/upload', Upload.fields ([{name:'file1'}, {name:'file2'}, {name:'File3'}, {name:'file4'}, {name:'File5'}]), function (req, res, next) { for(varIinchreq.files) {Console.log (req.files[i]); } req.flash ('Success','File Upload successful!'); Res.redirect ('/upload');});
Yes, baby, you can upload, but after the upload generated is a randomly generated name, this can not have,
Reference Official Document: Https://github.com/expressjs/multer
var storage=Multer.Diskstorage ({Destination:function (Req, file, CB) { CB (null, '/tmp/my-uploads')}, filename: function (req, C12>file, CB) { CB (null, file. FieldName + '-' + Date. Now ())}})var upload = multer ({storage: storage})
Then modify the code:
var multer = require (' Multer '), var storage = Multer.diskstorage ({ destination:function (req, file, CB) { CB (NULL , './public/images/'); }, filename:function (req, file, CB) { CB (NULL, file.originalname);} ) var upload = Multer ({storage:storage}); App.post ('/upload ', checklogin); App.post ('/upload ', upload.fields ([ {name: ' File1 '}, {name: ' File2 '}, {name: ' File3 '}, {name: ' File4 '}, {name: ' File5 '} ]), function (req, res, next) {for (var i in req.files) { Console.log ( Req.files[i]); } Req.flash (' Success ', ' File upload succeeded! '); Res.redirect ('/upload '); });
Kiss, you'll be all right,
Version Ah, version, remember, remember.
The multer of node. js