Monday, 15 April 2013

node.js - Nodejs send data in gzip using zlib -



node.js - Nodejs send data in gzip using zlib -

i tried send text in gzip, don't know how. in examples code uses fs, don't want send text file, string.

var zlib = require('zlib'); var http = require('http'); http.createserver(function (req, res) { res.writehead(200, {'content-type': 'text/html', 'content-encoding': 'gzip'}); var text = "hello world!"; res.end(text); }).listen(80);

you're half way there. can heartily agree documentation isn't quite snuff on how this;

class="lang-js prettyprint-override">var zlib = require('zlib'); var http = require('http'); http.createserver(function (req, res) { res.writehead(200, {'content-type': 'text/html', 'content-encoding': 'gzip'}); var text = "hello world!"; var buf = new buffer(text, 'utf-8'); // take encoding string. zlib.gzip(buf, function (_, result) { // callback give res.end(result); // result, send it. }); }).listen(80);

a simplification not utilize buffer;

http.createserver(function (req, res) { res.writehead(200, {'content-type': 'text/html', 'content-encoding': 'gzip'}); var text = "hello world!"; zlib.gzip(text, function (_, result) { // callback give res.end(result); // result, send it. }); }).listen(80);

...and seems send utf-8 default. however, prefer walk on safe side when there no default behavior makes more sense others , can't confirm documentation.

node.js gzip

No comments:

Post a Comment