为什么当我在下一个中间件函数中包含 res.send 时图像不渲染
why image is not rendering when i am including a res.send in next middleware function
我是expressjs的新手,
这是我的应用程序 js:当我删除下一个中间件中的 res.send 即 Aman 时,图像正在下载,但是当我包含它时,图像没有下载。我很困惑我的意思是中间件一个接一个地执行,一个被执行然后 next() 将被调用然后是 next 但这里不是那样的,为什么会这样。?谁能给我解释一下
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");
});
使用 res.sendFile()
提供来自 public 文件夹
的文件
/* Public static directory */
app.use(express.static(__dirname + '/public'));
/* Serve image for example inside the assets folder*/
app.get('/download', function(req, res){
res.sendFile('/assets/image.png');
});
由于 res.download(__dirname + '/instagram_5.jpg')
是异步进程,因此 next()
在 res.download
在后台工作时执行。
所以你甚至不需要调用 res.send()
和 next()
因为 res.download()
完成后它会自动响应客户端。
var express = require('express');
var app = express();
/* Public directory */
app.use("/images", express.static(__dirname + '/public/images'));
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', function(req, res, err){
if (err) {
return res.send();
}
var file = __dirname + '/images/image.png'; //here comes your path
var filename = path.basename(file);
var mimetype = mime.lookup(file);
res.setHeader('Content-disposition', 'attachment; filename=' + filename);
res.setHeader('Content-type', mimetype);
var filestream = fs.createReadStream(file);
filestream.pipe(res);
});
app.use(function(req, res) {
res.status(404).send("Sorry cant find that");
});
app.listen(4000, function() {
console.log("App started on port 4000");
});
我是expressjs的新手, 这是我的应用程序 js:当我删除下一个中间件中的 res.send 即 Aman 时,图像正在下载,但是当我包含它时,图像没有下载。我很困惑我的意思是中间件一个接一个地执行,一个被执行然后 next() 将被调用然后是 next 但这里不是那样的,为什么会这样。?谁能给我解释一下
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");
});
使用 res.sendFile()
提供来自 public 文件夹
/* Public static directory */
app.use(express.static(__dirname + '/public'));
/* Serve image for example inside the assets folder*/
app.get('/download', function(req, res){
res.sendFile('/assets/image.png');
});
由于 res.download(__dirname + '/instagram_5.jpg')
是异步进程,因此 next()
在 res.download
在后台工作时执行。
所以你甚至不需要调用 res.send()
和 next()
因为 res.download()
完成后它会自动响应客户端。
var express = require('express');
var app = express();
/* Public directory */
app.use("/images", express.static(__dirname + '/public/images'));
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', function(req, res, err){
if (err) {
return res.send();
}
var file = __dirname + '/images/image.png'; //here comes your path
var filename = path.basename(file);
var mimetype = mime.lookup(file);
res.setHeader('Content-disposition', 'attachment; filename=' + filename);
res.setHeader('Content-type', mimetype);
var filestream = fs.createReadStream(file);
filestream.pipe(res);
});
app.use(function(req, res) {
res.status(404).send("Sorry cant find that");
});
app.listen(4000, function() {
console.log("App started on port 4000");
});