Reputation: 15104
I am able to upload images using below in node JS.
var express = require('express');
var router = express.Router();
var multer = require('multer');
var done = false;
router.get( '/upload', function(req , res) {
res.render('upload');
});
router.post( '/upload' , multer({ dest: './uploadImages/'}).single('idUpload') , function(req , res) {
//-- logic for uploading file comes here
res.send('done');
});
module.exports = router;
As per my understanding a middleware can have 2 parameters , first the path('/upload') and the second call back(function(req , res).
But in the above code middleware has three paramters , second one being "multer({ dest: './uploadImages/'}).single('idUpload')"
I would like to know how many paramters are allowed for a middleware function in node/express JS.
Upvotes: 2
Views: 545
Reputation: 1787
A middleware is of the form
var first = function(req, res, next){
req.count = 1; //do something in this case attach count item to req object
next(); // call next middleware
}
var second = function(req, res, next){
req.count += 1;
next();
}
if we want to use above two middlewares in post route you can use them as
router.post('/mypath', first, second, function(req, res){
res.send(req.count) // will show 2 on response
}
as in above case we could have used more middlewares.
now comming to router.post()
it accepts path as first argument as path and any number of middlewares.
As for the upload problem make sure you have uploadImages
directory and its path is correct and request header has Content-Type: 'multipat/form-data' with file field name being idUpload
Upvotes: 3