Node.JS deflate / gzip响应文本 [英] Node.JS deflate/gzip response text

查看:56
本文介绍了Node.JS deflate / gzip响应文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到显示的示例此处

response.writeHead(200, { 'content-encoding': 'deflate' });
raw.pipe(zlib.createDeflate()).pipe(response);

我试图通过创建读取流并发送缩小的输出来发送响应。

I tried to send the response by creating a read stream and sending deflated output.

但是我正在寻找一种没有读取流的方法,因为我的文本来自另一个 HTTP 请求。我只是习惯于 response.write(text);

But I am looking for a way to do it without read stream, because the text I have comes from another HTTP request. I simply used to response.write(text);

我尝试了几件事,包括

zlib.deflate(text, function (err, buffer) {
    if (err) throw err;
    response.write(buffer);
    response.end();
});

但是浏览器说无效或不受支持的压缩形式。

我做错了什么?

connect 的代码说

return stream
    ? stream.write(new Buffer(chunk, encoding))
    : write.call(res, chunk, encoding);

那么只会处理流吗?

推荐答案

您使用的是Express吗?如果是这样,只需使用 compress 中间件:

Are you using Express? If so, just use the compress middleware:

app.use(express.compress());

http://expressjs.com/api.html#compress

这篇关于Node.JS deflate / gzip响应文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆