Is there a way of piping the resized image to my express response?
有没有办法将调整大小的图像用于表达我的快速反应?
Something along the lines of:
有点像:
var express = require('express'),
app = express.createServer();
app.get('/', function(req, res){
gm('images/test.jpg')
.resize(50,50)
.stream(function streamOut (err, stdout, stderr) {
if (err) return finish(err);
stdout.pipe(res.end, { end: false }); //suspect error is here...
stdout.on('end', function(){res.writeHead(200, { 'Content-Type': 'ima ge/jpeg' });});
stdout.on('error', finish);
stdout.on('close', finish);
});
});
app.listen(3000);
This unfortunately causes an error...
Pretty sure I've got some syntax wrong.
不幸的是,这会导致错误...很确定我的语法有些错误。
2 个解决方案
#1
13
your question actually helped me get the answer for the same issue. How it worked for me:
你的问题实际上帮助我得到了同样问题的答案。它对我有用:
var express = require('express'),
app = express.createServer();
app.get('/', function(req, res, next){
gm('images/test.jpg')
.resize(50,50)
.stream(function streamOut (err, stdout, stderr) {
if (err) return next(err);
stdout.pipe(res); //pipe to response
// the following line gave me an error compaining for already sent headers
//stdout.on('end', function(){res.writeHead(200, { 'Content-Type': 'ima ge/jpeg' });});
stdout.on('error', next);
});
});
app.listen(3000);
I removed all reference to finish function as it's not defined and sent the error to express error handler. Hope it helps someone. i also would like to add i'm using express 3, so creating the server is slightly different.
我删除了所有对finish函数的引用,因为它没有定义,并发送错误来表示错误处理程序。希望它可以帮到某人。我也想添加我使用快递3,所以创建服务器略有不同。
#2
5
You get the error because you want to write to res
after you've piped the image. Try setting the headers before you pipe your image:
您收到错误是因为您想在管道传输图像后写入res。在管道图像之前尝试设置标题:
var express = require('express'),
app = express.createServer();
app.get('/', function(req, res){
res.set('Content-Type', 'image/jpeg'); // set the header here
gm('images/test.jpg')
.resize(50,50)
.stream(function (err, stdout, stderr) {
stdout.pipe(res)
});
});
app.listen(3000);
#1
13
your question actually helped me get the answer for the same issue. How it worked for me:
你的问题实际上帮助我得到了同样问题的答案。它对我有用:
var express = require('express'),
app = express.createServer();
app.get('/', function(req, res, next){
gm('images/test.jpg')
.resize(50,50)
.stream(function streamOut (err, stdout, stderr) {
if (err) return next(err);
stdout.pipe(res); //pipe to response
// the following line gave me an error compaining for already sent headers
//stdout.on('end', function(){res.writeHead(200, { 'Content-Type': 'ima ge/jpeg' });});
stdout.on('error', next);
});
});
app.listen(3000);
I removed all reference to finish function as it's not defined and sent the error to express error handler. Hope it helps someone. i also would like to add i'm using express 3, so creating the server is slightly different.
我删除了所有对finish函数的引用,因为它没有定义,并发送错误来表示错误处理程序。希望它可以帮到某人。我也想添加我使用快递3,所以创建服务器略有不同。
#2
5
You get the error because you want to write to res
after you've piped the image. Try setting the headers before you pipe your image:
您收到错误是因为您想在管道传输图像后写入res。在管道图像之前尝试设置标题:
var express = require('express'),
app = express.createServer();
app.get('/', function(req, res){
res.set('Content-Type', 'image/jpeg'); // set the header here
gm('images/test.jpg')
.resize(50,50)
.stream(function (err, stdout, stderr) {
stdout.pipe(res)
});
});
app.listen(3000);