Nodejs about gzip/deflate Compression _node.js

Source: Internet
Author: User

0x01. About

When you write HTTP, when you receive HTTP requests, there are garbled, and later found that Gzip did not extract.

With regard to gzip/deflate compression, there is a method of placing pipe compression and non piping compression.

0x02. Pipeline compression

I/O in node is asynchronous, so reading and writing to disk and network requires a callback function to read the data.

We need to use the data stream when it is not possible to load the data that needs to be processed at once in memory, or when the processing is more efficient while reading one side.

NODEJS provides operations on the flow of data through a variety of stream streams.

The official website provides the piping method:

Copy Code code as follows:

Client Request Example
var zlib = require (' zlib ');
var http = require (' http ');
var fs = require (' FS ');
var request = Http.get ({host: ' Homeway.me ',
Path: '/',
PORT:80,
Headers: {' accept-encoding ': ' Gzip,deflate '}});
Request.on (' response ', function (response) {
var output = Fs.createwritestream (' izs.me_index.html ');
Switch (response.headers[' content-encoding ']) {
Or, just use Zlib.createunzip () to handle both cases
Case ' gzip ':
Response.pipe (Zlib.creategunzip ()). pipe (output);
Break
Case ' deflate ':
Response.pipe (Zlib.createinflate ()). pipe (output);
Break
Default
Response.pipe (output);
Break
}
});

0x03. Non-piping compression

The code is as follows:

Copy Code code as follows:

#! /usr/local/bin/node
var http = require (' http '),
QueryString = Require (' querystring '),
Zlib = require (' zlib ');
var args = {
Parameters and Alternate Data
Contents:querystring.stringify ({
Information on the contract
Name: ' Homeway.me ',
}),
};
var options = {
Hostname: ' homeway.me ',
PORT:80,
Path: '/',
Method: ' Get ',
Headers: {
' Accept ': ' text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 ',
' Content-length ': args.contents.length,
' User-agent ': ' mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) applewebkit/537.36 (khtml, like Gecko) chrome/42.0.2311.11 safari/537.36 ',
' accept-encoding ': ' gzip, deflate ',
},
};
var get = function (options, args, callback) {
var req = http.request (options, function (res) {
var chunks =[], data, encoding = res.headers[' content-encoding '];
Non-gzip/deflate to be turned into utf-8 format
if (encoding = = ' undefined ') {
Res.setencoding (' Utf-8 ');
}
Res.on (' Data ', function (chunk) {
Chunks.push (chunk);
});
Res.on (' End ', function () {
var buffer = Buffer.concat (chunks);
if (encoding = = ' gzip ') {
Zlib.gunzip (buffer, function (err, decoded) {
data = Decoded.tostring ();
Callback (err, args, res.headers, data);
});
else if (encoding = = ' deflate ') {
Zlib.inflate (buffer, function (err, decoded) {
data = Decoded.tostring ();
Callback (err, args, res.headers, data);
});
} else {
data = Buffer.tostring ();
Callback (null, args, res.headers, data);
}
});
});
Req.write (args.contents);
Req.end ();
};
Get (options, args, function (err, args, headers, data) {
Console.log (' ==>header \ n ', headers);
Console.log (' ==data \ n ', data);
});

The above is nodejs about gzip/deflate compression of the entire content, I hope you can like.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.