I am newbie to expressjs, Here is my app js: When i am removing the res.send i.e Aman in next middleware the image is downloading but when i am including that , image is not downloading. I am confused i mean middleware executes one by one , one is executed then next() will evoke and then next but here it is not like that , Why is it so .? Can anyone explain this to me
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send("Default");
});
var Aperson = function(req, res, next) {
console.log("File is downloaing");
res.download(__dirname + '/instagram_5.jpg');
next();
}
app.get("/download", [Aperson], function(req, res) {
res.send("AMan");
});
app.use(function(req, res) {
res.status(404).send("Sorry cant find that");
});
app.listen(4000, function() {
console.log("App started on port 4000");
});
Since res.download(__dirname + '/instagram_5.jpg')
is async process the next()
is executed while the res.download
is working in background.
So you not need to call even res.send()
and next()
because after res.download()
finishes it will respond to the client automatically.