24

I tried to send the text in gzip, but I don't know how. In the examples the code uses fs, but I don't want to send a text file, just a string.

const zlib = require('zlib');
const http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html', 'Content-Encoding': 'gzip'});

    const text = "Hello World!";
    res.end(text);

}).listen(80);
Ravi Kumar Gupta
  • 1,698
  • 2
  • 22
  • 38
friction
  • 787
  • 2
  • 10
  • 15

1 Answers1

50

You're half way there. I can heartily agree that the documentation isn't quite up to snuff on how to do this;

const zlib = require('zlib');
const http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html', 'Content-Encoding': 'gzip'});

    const text = "Hello World!";
    const buf = new Buffer(text, 'utf-8');   // Choose encoding for the string.
    zlib.gzip(buf, function (_, result) {  // The callback will give you the 
        res.end(result);                     // result, so just send it.
    });
}).listen(80);

A simplification would be not to use the Buffer;

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html', 'Content-Encoding': 'gzip'});

    const text = "Hello World!";
    zlib.gzip(text, function (_, result) {  // The callback will give you the 
      res.end(result);                     // result, so just send it.
    });
}).listen(80);

...and it seems to send UTF-8 by default. However, I personally prefer to walk on the safe side when there is no default behavior that makes more sense than others and I can't immediately confirm it with documentation.

Similarly, in case you need to pass a JSON object instead:

const data = {'hello':'swateek!'}

res.writeHead(200, {'Content-Type': 'application/json', 'Content-Encoding': 'gzip'});
const buf = new Buffer(JSON.stringify(data), 'utf-8');
zlib.gzip(buf, function (_, result) {
    res.end(result);
});
Ravi Kumar Gupta
  • 1,698
  • 2
  • 22
  • 38
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • What does the value of `_` represent in the callback? I'm thinking maybe an error, but can't find it documented... – cprcrack May 27 '14 at 14:17
  • 1
    @cprcrack It's just an unused parameter to the callback. `_` is valid as a variable/parameter name and I use it as a marker that makes it quite obvious (to me) that it's not used. – Joachim Isaksson May 27 '14 at 15:34
  • I get the idea, but still would like to know why the callback caller is using that parameter and whether it may sometimes be used/useful. – cprcrack May 27 '14 at 16:11
  • 1
    @cprcrack The in this case ignored parameter is `error`, that is, if gzip fails to compress the buffer, you'll get the error passed back there. All zlib [convenience methods](http://nodejs.org/api/zlib.html#zlib_convenience_methods) pass (error, result) back as parameters to the callback. – Joachim Isaksson May 27 '14 at 16:43
  • 7
    As a heads up, you should use `Buffer.from` when converting a string to a buffer instead of `new Buffer`. `new Buffer` has been deprecated. https://nodejs.org/api/buffer.html#buffer_new_buffer_str_encoding – Vinay May 21 '16 at 06:08
  • I had to convert a JSON object to buffer, so this is how I did it: var data = {'hello':'swateek!'} res.writeHead(200, {'Content-Type': 'application/json', 'Content-Encoding': 'gzip'}); var buf = new Buffer(JSON.stringify(data), 'utf-8'); zlib.gzip(buf, function (_, result) { // The callback will give you the result, so just send it. res.end(result); }); – swateek Oct 26 '17 at 18:45