Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use zlib in Node

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge about "how to use zlib in Node". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

overview

Students who have done web performance optimization should be familiar with the performance optimization killer gzip. The browser initiates a resource request to the server, such as downloading a JS file, and the server compresses the resource first and then returns it to the browser to save traffic and speed up access.

The browser adds Accept-Encoding to the HTTP request header, telling the server,"You can compress resources using gzip or defalte algorithms."

Accept-Encoding:gzip, deflate

So, how does nodejs compress resources? The answer is the Zlib module.

Getting Started Example: Simple Compression/Decompression

Examples of compression

Very simple few lines of code to complete the gzip compression of local files.

var fs = require('fs'); var zlib = require('zlib'); var gzip = zlib.createGzip(); var inFile = fs.createReadStream('./ extra/fileForCompress.txt'); var out = fs.createWriteStream('./ extra/fileForCompress.txt.gz'); inFile.pipe(gzip).pipe(out);

Examples of decompression

It's also very simple, it's a reverse operation.

var fs = require('fs'); var zlib = require('zlib'); var gunzip = zlib.createGunzip(); var inFile = fs.createReadStream('./ extra/fileForCompress.txt.gz'); var outFile = fs.createWriteStream('./ extra/fileForCompress1.txt'); inFile.pipe(gunzip).pipe(outFile);

Server gzip compression

The code is super simple. First, determine whether it contains an accept-encoding header, and the value is gzip.

No: Return uncompressed files.

Yes: Returns gzip compressed files.

var http = require('http'); var zlib = require('zlib'); var fs = require('fs'); var filepath = './ extra/fileForGzip.html'; var server = http.createServer(function(req, res){ var acceptEncoding = req.headers['accept-encoding']; var gzip; if(acceptEncoding.indexOf('gzip')!=- 1){ //determine if gzip compression is required gzip = zlib.createGzip(); //Remember to respond Content-Encoding, telling the browser that the file has been compressed by gzip res.writeHead(200, { 'Content-Encoding': 'gzip' }); fs.createReadStream(filepath).pipe(gzip).pipe(res); }else{ fs.createReadStream(filepath).pipe(res); } }); server.listen('3000');

Server string gzip compression

The code is similar to the previous example. Here slib.gzipSync(str) is used to gzip the string.

var http = require('http'); var zlib = require('zlib'); var responseText = 'hello world'; var server = http.createServer(function(req, res){ var acceptEncoding = req.headers['accept-encoding']; if(acceptEncoding.indexOf('gzip')!=- 1){ res.writeHead(200, { 'content-encoding': 'gzip' }); res.end( zlib.gzipSync(responseText) ); }else{ res.end(responseText); } }); server.listen ('3000 ');"How to use zlib in Node" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report