{"id":55747,"date":"2025-08-14T07:44:42","date_gmt":"2025-08-13T23:44:42","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/55747.html"},"modified":"2025-08-14T07:44:42","modified_gmt":"2025-08-13T23:44:42","slug":"node-js-%e4%b8%ad%e7%9a%84-gzip-%e5%8e%8b%e7%bc%a9%ef%bc%9a%e5%8a%a0%e9%80%9f%e4%bd%a0%e7%9a%84-web-%e5%ba%94%e7%94%a8%e4%bc%a0%e8%be%93","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/55747.html","title":{"rendered":"Node.js \u4e2d\u7684 Gzip \u538b\u7f29\uff1a\u52a0\u901f\u4f60\u7684 Web \u5e94\u7528\u4f20\u8f93"},"content":{"rendered":"<p>\u5728\u73b0\u4ee3 Web \u5f00\u53d1\u4e2d&#xff0c;\u6027\u80fd\u4f18\u5316\u662f\u6c38\u6052\u7684\u4e3b\u9898\u3002\u4f60\u662f\u5426\u66fe\u56e0\u7f51\u7ad9\u52a0\u8f7d\u7f13\u6162\u800c\u5931\u53bb\u7528\u6237&#xff1f;\u5176\u4e2d\u4e00\u4e2a\u5173\u952e\u74f6\u9888\u5c31\u662f\u6570\u636e\u4f20\u8f93\u91cf\u3002Node.js \u4f5c\u4e3a\u9ad8\u6027\u80fd\u7684\u540e\u7aef\u5e73\u53f0&#xff0c;\u7ed3\u5408 Gzip \u538b\u7f29\u6280\u672f&#xff0c;\u80fd\u663e\u8457\u51cf\u5c11\u7f51\u7edc\u4f20\u8f93\u65f6\u95f4&#xff0c;\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<h3>&#x1f50d; \u4e3a\u4ec0\u4e48\u9700\u8981 Gzip \u538b\u7f29&#xff1f;<\/h3>\n<ul>\n<li>\u5e26\u5bbd\u6d6a\u8d39&#xff1a; \u6587\u672c\u8d44\u6e90&#xff08;HTML\u3001CSS\u3001JS\u3001JSON&#xff09;\u5305\u542b\u5927\u91cf\u5197\u4f59\u4fe1\u606f\u3002<\/li>\n<li>\u52a0\u8f7d\u5ef6\u8fdf&#xff1a; \u5927\u6587\u4ef6\u4f20\u8f93\u8017\u65f6&#xff0c;\u5c24\u5176\u5728\u79fb\u52a8\u7f51\u7edc\u73af\u5883\u4e0b\u66f4\u660e\u663e\u3002<\/li>\n<li>\u6210\u672c\u589e\u52a0&#xff1a; \u670d\u52a1\u5668\u5e26\u5bbd\u548c CDN \u6d41\u91cf\u8d39\u7528\u53ef\u80fd\u56e0\u6b64\u4e0a\u5347\u3002<\/li>\n<\/ul>\n<p>Gzip \u57fa\u4e8e DEFLATE \u7b97\u6cd5&#xff08;\u7ed3\u5408 LZ77 \u548c\u54c8\u592b\u66fc\u7f16\u7801&#xff09;&#xff0c;\u80fd\u6709\u6548\u5c06\u6587\u672c\u5185\u5bb9\u538b\u7f29\u81f3\u539f\u5927\u5c0f\u7684 30% \u751a\u81f3\u66f4\u4f4e&#xff0c;\u5bf9\u56fe\u7247\u7b49\u4e8c\u8fdb\u5236\u683c\u5f0f\u6548\u679c\u6709\u9650\u3002<\/p>\n<h3>&#x1f9e9; Node.js \u4e2d\u7684 Gzip \u5e94\u7528\u573a\u666f<\/h3>\n<h4>1\ufe0f\u20e3 \u670d\u52a1\u5668\u7aef\u538b\u7f29\u54cd\u5e94 (\u6700\u5e38\u89c1)<\/h4>\n<p>\u8fd9\u662f\u6700\u666e\u904d\u7684\u4f18\u5316\u624b\u6bb5\u3002\u5f53 Node.js \u670d\u52a1\u5668\u53d1\u9001\u6587\u672c\u54cd\u5e94&#xff08;\u5982 HTML\u3001CSS\u3001JS\u3001JSON&#xff09;\u7ed9\u6d4f\u89c8\u5668\u65f6&#xff0c;\u5b9e\u65f6\u8fdb\u884c Gzip \u538b\u7f29\u3002<\/p>\n<p>\u4f7f\u7528 zlib \u6a21\u5757 &#043; Express \u4e2d\u95f4\u4ef6&#xff1a;<\/p>\n<p><span class=\"token keyword\">const<\/span> express <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;express&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> compression <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;compression&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u4e13\u95e8\u7528\u4e8e\u538b\u7f29\u7684\u4e2d\u95f4\u4ef6<\/span><\/p>\n<p><span class=\"token keyword\">const<\/span> app <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">express<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4f7f\u7528 compression \u4e2d\u95f4\u4ef6<\/span><br \/>\napp<span class=\"token punctuation\">.<\/span><span class=\"token function\">use<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">compression<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><br \/>\n  level<span class=\"token operator\">:<\/span> <span class=\"token number\">6<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token comment\">\/\/ \u538b\u7f29\u7ea7\u522b (1-9, 6 \u662f\u8f83\u597d\u7684\u5e73\u8861\u70b9)<\/span><br \/>\n  threshold<span class=\"token operator\">:<\/span> <span class=\"token number\">1024<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token comment\">\/\/ \u53ea\u538b\u7f29\u5927\u4e8e 1KB \u7684\u54cd\u5e94<\/span><br \/>\n  <span class=\"token function-variable function\">filter<\/span><span class=\"token operator\">:<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token parameter\">req<span class=\"token punctuation\">,<\/span> res<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u53ef\u9009&#xff1a;\u6839\u636e\u8bf7\u6c42\u6216\u54cd\u5e94\u51b3\u5b9a\u662f\u5426\u538b\u7f29<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>req<span class=\"token punctuation\">.<\/span>headers<span class=\"token punctuation\">[<\/span><span class=\"token string\">&#039;x-no-compression&#039;<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> compression<span class=\"token punctuation\">.<\/span><span class=\"token function\">filter<\/span><span class=\"token punctuation\">(<\/span>req<span class=\"token punctuation\">,<\/span> res<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4f60\u7684\u8def\u7531<\/span><br \/>\napp<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\/&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token parameter\">req<span class=\"token punctuation\">,<\/span> res<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  res<span class=\"token punctuation\">.<\/span><span class=\"token function\">send<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u8fd9\u4e2a\u54cd\u5e94\u5c06\u88ab\u81ea\u52a8\u538b\u7f29&#xff01;&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>app<span class=\"token punctuation\">.<\/span><span class=\"token function\">listen<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3000<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u5173\u952e\u70b9&#xff1a;<\/p>\n<ul>\n<li>compression \u4e2d\u95f4\u4ef6\u7b80\u5316\u4e86\u6d41\u7a0b&#xff0c;\u81ea\u52a8\u5904\u7406 Content-Encoding \u5934\u3002<\/li>\n<li>\u538b\u7f29\u7ea7\u522b (level)&#xff1a;\u8d8a\u9ad8\u538b\u7f29\u6bd4\u8d8a\u5927&#xff0c;\u4f46 CPU \u6d88\u8017\u4e5f\u8d8a\u9ad8\u3002<\/li>\n<li>\u9608\u503c (threshold)&#xff1a;\u907f\u514d\u538b\u7f29\u8fc7\u5c0f\u6587\u4ef6&#xff08;\u538b\u7f29\u6536\u76ca\u5c0f&#xff0c;\u53cd\u800c\u589e\u52a0\u5f00\u9500&#xff09;\u3002<\/li>\n<li>\u8fc7\u6ee4\u5668 (filter)&#xff1a;\u53ef\u5b9a\u5236\u903b\u8f91&#xff0c;\u4f8b\u5982\u4e0d\u538b\u7f29\u7279\u5b9a\u7c7b\u578b\u6216\u6765\u6e90\u7684\u8bf7\u6c42\u3002<\/li>\n<li>\u4e2d\u95f4\u4ef6\u5e94\u5c3d\u91cf\u653e\u5728\u5176\u4ed6\u4e2d\u95f4\u4ef6\u4e4b\u524d&#xff0c;\u786e\u4fdd\u538b\u7f29\u6700\u5148\u6267\u884c\u3002<\/li>\n<\/ul>\n<h4>2\ufe0f\u20e3 \u5ba2\u6237\u7aef\u5904\u7406\u538b\u7f29\u6570\u636e<\/h4>\n<p>Node.js \u670d\u52a1\u5668\u4e5f\u53ef\u80fd\u9700\u8981\u5904\u7406\u5ba2\u6237\u7aef\u53d1\u9001\u7684 Gzip \u538b\u7f29\u8bf7\u6c42\u4f53&#xff08;\u8f83\u5c11\u89c1&#xff0c;\u4f46\u67d0\u4e9b API \u53ef\u80fd\u4f7f\u7528&#xff09;\u3002<\/p>\n<p><span class=\"token keyword\">const<\/span> http <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;http&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> zlib <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;zlib&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">const<\/span> server <span class=\"token operator\">&#061;<\/span> http<span class=\"token punctuation\">.<\/span><span class=\"token function\">createServer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">req<span class=\"token punctuation\">,<\/span> res<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>req<span class=\"token punctuation\">.<\/span>headers<span class=\"token punctuation\">[<\/span><span class=\"token string\">&#039;content-encoding&#039;<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;&#061;&#061;<\/span> <span class=\"token string\">&#039;gzip&#039;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">const<\/span> gunzip <span class=\"token operator\">&#061;<\/span> zlib<span class=\"token punctuation\">.<\/span><span class=\"token function\">createGunzip<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">let<\/span> uncompressedData <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    req<span class=\"token punctuation\">.<\/span><span class=\"token function\">pipe<\/span><span class=\"token punctuation\">(<\/span>gunzip<span class=\"token punctuation\">)<\/span><br \/>\n      <span class=\"token punctuation\">.<\/span><span class=\"token function\">on<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;data&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token parameter\">chunk<\/span> <span class=\"token operator\">&#061;&gt;<\/span> uncompressedData <span class=\"token operator\">&#043;&#061;<\/span> chunk<span class=\"token punctuation\">)<\/span><br \/>\n      <span class=\"token punctuation\">.<\/span><span class=\"token function\">on<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;end&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">try<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n          <span class=\"token comment\">\/\/ \u5904\u7406\u89e3\u538b\u540e\u7684\u6570\u636e (uncompressedData)<\/span><br \/>\n          console<span class=\"token punctuation\">.<\/span><span class=\"token function\">log<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;Received data:&#039;<\/span><span class=\"token punctuation\">,<\/span> uncompressedData<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n          res<span class=\"token punctuation\">.<\/span><span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;Data received and decompressed&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">catch<\/span> <span class=\"token punctuation\">(<\/span>err<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n          res<span class=\"token punctuation\">.<\/span>statusCode <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">400<\/span><span class=\"token punctuation\">;<\/span><br \/>\n          res<span class=\"token punctuation\">.<\/span><span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;Invalid data&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n      <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><br \/>\n      <span class=\"token punctuation\">.<\/span><span class=\"token function\">on<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;error&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token parameter\">err<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        res<span class=\"token punctuation\">.<\/span>statusCode <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">400<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        res<span class=\"token punctuation\">.<\/span><span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;Decompression error&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n      <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5904\u7406\u672a\u538b\u7f29\u7684\u8bf7\u6c42<\/span><br \/>\n    <span class=\"token comment\">\/\/ &#8230;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>server<span class=\"token punctuation\">.<\/span><span class=\"token function\">listen<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3000<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u5173\u952e\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u68c0\u67e5\u8bf7\u6c42\u5934 Content-Encoding: gzip\u3002<\/li>\n<li>\u4f7f\u7528 zlib.createGunzip() \u521b\u5efa\u89e3\u538b\u6d41\u3002<\/li>\n<li>\u901a\u8fc7\u7ba1\u9053 (pipe) \u5c06\u8bf7\u6c42\u6d41\u5bfc\u5165\u89e3\u538b\u6d41\u3002<\/li>\n<li>\u76d1\u542c data \u4e8b\u4ef6\u6536\u96c6\u89e3\u538b\u540e\u7684\u6570\u636e\u5757\u3002<\/li>\n<li>\u5728 end \u4e8b\u4ef6\u4e2d\u5904\u7406\u5b8c\u6574\u89e3\u538b\u6570\u636e\u3002<\/li>\n<li>\u52a1\u5fc5\u5904\u7406 error \u4e8b\u4ef6&#xff0c;\u9632\u6b62\u65e0\u6548\u538b\u7f29\u6570\u636e\u5bfc\u81f4\u5d29\u6e83\u3002<\/li>\n<\/ul>\n<h3>\u2699\ufe0f \u6027\u80fd\u4e0e\u4f18\u5316\u5efa\u8bae<\/h3>\n<li>\u9009\u62e9\u5408\u9002\u7684\u538b\u7f29\u7ea7\u522b&#xff1a; \u9ed8\u8ba4\u7ea7\u522b 6 \u901a\u5e38\u662f\u6700\u4f73\u5e73\u8861\u70b9\u3002\u7ea7\u522b 9 \u538b\u7f29\u7387\u63d0\u5347\u6709\u9650&#xff0c;\u4f46 CPU \u5f00\u9500\u663e\u8457\u589e\u52a0\u3002<\/li>\n<li>\u907f\u514d\u538b\u7f29\u5df2\u538b\u7f29\u5185\u5bb9&#xff1a; \u4e0d\u8981\u5bf9\u56fe\u7247&#xff08;JPEG\u3001PNG&#xff09;\u3001\u89c6\u9891\u3001\u97f3\u9891\u6216\u5df2\u538b\u7f29\u6587\u4ef6&#xff08;\u5982 .zip&#xff09;\u8fdb\u884c Gzip \u538b\u7f29&#xff0c;\u6548\u679c\u5fae\u4e4e\u5176\u5fae\u4e14\u6d6a\u8d39 CPU\u3002<\/li>\n<li>\u5229\u7528\u7f13\u5b58&#xff1a; \u5bf9\u9759\u6001\u6587\u4ef6&#xff08;\u5982 CSS\u3001JS&#xff09;\u8fdb\u884c\u538b\u7f29\u540e&#xff0c;\u5e94\u8bbe\u7f6e\u5f3a\u7f13\u5b58 (Cache-Control, ETag)&#xff0c;\u907f\u514d\u91cd\u590d\u538b\u7f29\u3002<\/li>\n<li>\u8003\u8651\u53cd\u5411\u4ee3\u7406&#xff1a; \u751f\u4ea7\u73af\u5883\u4e2d&#xff0c;\u5e38\u4f7f\u7528 Nginx \u6216 Apache \u4f5c\u4e3a Node.js \u7684\u53cd\u5411\u4ee3\u7406\u3002\u5b83\u4eec\u901a\u5e38\u5185\u7f6e\u9ad8\u6548 Gzip \u6a21\u5757&#xff0c;\u53ef\u5c06\u538b\u7f29\u4efb\u52a1\u5378\u8f7d\u7ed9\u5b83\u4eec&#xff0c;\u51cf\u8f7b Node.js \u8fdb\u7a0b\u8d1f\u62c5\u3002\u786e\u4fdd Node.js \u5e94\u7528\u672c\u8eab\u4e0d\u518d\u8fdb\u884c\u538b\u7f29&#xff0c;\u907f\u514d\u53cc\u91cd\u538b\u7f29\u3002<\/li>\n<li>\u6743\u8861 CPU \u4e0e\u5e26\u5bbd&#xff1a; \u9ad8\u6d41\u91cf\u573a\u666f\u4e0b&#xff0c;\u538b\u7f29\u6d88\u8017 CPU\u3002\u76d1\u63a7\u670d\u52a1\u5668\u8d1f\u8f7d&#xff0c;\u5fc5\u8981\u65f6\u964d\u4f4e\u538b\u7f29\u7ea7\u522b\u6216\u5bf9\u7279\u5b9a\u8d44\u6e90\u505c\u7528\u538b\u7f29\u3002<\/li>\n<h3>&#x1f6e1;\ufe0f \u5b89\u5168\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ul>\n<li>\u89e3\u538b\u70b8\u5f39&#xff1a; \u6076\u610f\u7528\u6237\u53ef\u80fd\u53d1\u9001\u7cbe\u5fc3\u6784\u9020\u7684\u5c0f\u578b\u538b\u7f29\u6570\u636e&#xff0c;\u89e3\u538b\u540e\u4ea7\u751f\u5de8\u5927\u7684\u8f93\u51fa&#xff08;\u5982 42.zip \u7684\u4f20\u5947&#xff09;\u3002\u52a1\u5fc5&#xff1a;\n<ul>\n<li>\u4f7f\u7528 zlib \u7684 maxMemory \u9009\u9879\u9650\u5236\u89e3\u538b\u5185\u5b58\u3002<\/li>\n<li>\u5728\u53cd\u5411\u4ee3\u7406\u5c42\u9650\u5236\u8bf7\u6c42\u4f53\u5927\u5c0f\u3002<\/li>\n<li>\u5bf9\u89e3\u538b\u540e\u7684\u6570\u636e\u5927\u5c0f\u8fdb\u884c\u6821\u9a8c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span class=\"token keyword\">const<\/span> gunzip <span class=\"token operator\">&#061;<\/span> zlib<span class=\"token punctuation\">.<\/span><span class=\"token function\">createGunzip<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span> maxMemory<span class=\"token operator\">:<\/span> <span class=\"token number\">1024<\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">1024<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u9650\u5236\u4e3a 1MB \u5185\u5b58<\/span><\/p>\n<h3>&#x1f52e; \u8d85\u8d8a Gzip&#xff1a;Brotli<\/h3>\n<p>\u867d\u7136 Gzip \u4ecd\u662f\u4e3b\u6d41&#xff0c;\u4f46 Google \u5f00\u53d1\u7684 Brotli (br) \u538b\u7f29\u7b97\u6cd5\u901a\u5e38\u80fd\u63d0\u4f9b\u66f4\u9ad8\u7684\u538b\u7f29\u7387&#xff08;\u5c24\u5176\u5bf9\u6587\u672c&#xff09;&#xff0c;\u5c24\u5176\u9002\u5408\u9759\u6001\u8d44\u6e90\u3002Node.js \u7684 zlib \u6a21\u5757\u4e5f\u652f\u6301 Brotli (zlib.createBrotliCompress() \/ zlib.createBrotliDecompress())\u3002\u5176\u4f7f\u7528\u65b9\u5f0f\u4e0e Gzip \u7c7b\u4f3c&#xff0c;\u4f46\u9700\u6ce8\u610f\u5ba2\u6237\u7aef\u517c\u5bb9\u6027&#xff08;\u73b0\u4ee3\u6d4f\u89c8\u5668\u666e\u904d\u652f\u6301&#xff09;\u3002<\/p>\n<h3>&#x1f3af; \u603b\u7ed3<\/h3>\n<p>Gzip \u538b\u7f29\u662f\u63d0\u5347 Node.js Web \u5e94\u7528\u6027\u80fd\u6700\u7b80\u5355\u6709\u6548\u7684\u624b\u6bb5\u4e4b\u4e00\u3002\u901a\u8fc7 zlib \u6a21\u5757\u6216 compression \u4e2d\u95f4\u4ef6&#xff0c;\u4f60\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u54cd\u5e94\u538b\u7f29&#xff0c;\u663e\u8457\u51cf\u5c11\u4f20\u8f93\u65f6\u95f4\u3001\u8282\u7701\u5e26\u5bbd\u5e76\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002\u8bb0\u4f4f&#xff1a;<\/p>\n<ul>\n<li>\u4f18\u5148\u538b\u7f29\u6587\u672c\u8d44\u6e90\u3002<\/li>\n<li>\u9009\u62e9\u5408\u9002\u7684\u538b\u7f29\u7ea7\u522b\u548c\u9608\u503c\u3002<\/li>\n<li>\u5229\u7528\u7f13\u5b58\u907f\u514d\u91cd\u590d\u538b\u7f29\u3002<\/li>\n<li>\u751f\u4ea7\u73af\u5883\u8003\u8651\u7528\u53cd\u5411\u4ee3\u7406\u5904\u7406\u538b\u7f29\u3002<\/li>\n<li>\u89e3\u538b\u65f6\u6ce8\u610f\u5b89\u5168&#xff0c;\u9632\u8303\u89e3\u538b\u70b8\u5f39\u3002<\/li>\n<li>\u5173\u6ce8 Brotli \u4f5c\u4e3a\u66f4\u9ad8\u6548\u7684\u66ff\u4ee3\u65b9\u6848\u3002<\/li>\n<\/ul>\n<p>\u7acb\u5373\u4e3a\u4f60\u7684 Node.js \u5e94\u7528\u542f\u7528 Gzip&#xff0c;\u8ba9\u4f60\u7684\u7528\u6237\u4eab\u53d7\u98de\u4e00\u822c\u7684\u52a0\u8f7d\u901f\u5ea6&#xff01; &#x1f680;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e49\u6b21\uff0c\u6536\u85cf15\u6b21\u3002Gzip \u538b\u7f29\u662f\u63d0\u5347 Node.js Web \u5e94\u7528\u6027\u80fd\u6700\u7b80\u5355\u6709\u6548\u7684\u624b\u6bb5\u4e4b\u4e00\u3002\u901a\u8fc7zlib\u6a21\u5757\u6216\u4e2d\u95f4\u4ef6\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u54cd\u5e94\u538b\u7f29\uff0c\u663e\u8457\u51cf\u5c11\u4f20\u8f93\u65f6\u95f4\u3001\u8282\u7701\u5e26\u5bbd\u5e76\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002\u4f18\u5148\u538b\u7f29\u6587\u672c\u8d44\u6e90\u3002\u9009\u62e9\u5408\u9002\u7684\u538b\u7f29\u7ea7\u522b\u548c\u9608\u503c\u3002\u5229\u7528\u7f13\u5b58\u907f\u514d\u91cd\u590d\u538b\u7f29\u3002\u751f\u4ea7\u73af\u5883\u8003\u8651\u7528\u53cd\u5411\u4ee3\u7406\u5904\u7406\u538b\u7f29\u3002\u89e3\u538b\u65f6\u6ce8\u610f\u5b89\u5168\uff0c\u9632\u8303\u89e3\u538b\u70b8\u5f39\u3002\u5173\u6ce8 Brotli \u4f5c\u4e3a\u66f4\u9ad8\u6548\u7684\u66ff\u4ee3\u65b9\u6848\u3002\u7acb\u5373\u4e3a\u4f60\u7684 Node.js \u5e94\u7528\u542f\u7528 Gzip\uff0c\u8ba9\u4f60\u7684\u7528\u6237\u4eab\u53d7\u98de\u4e00\u822c\u7684\u52a0\u8f7d\u901f\u5ea6\uff01\ud83d\ude80\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[186,87],"topic":[],"class_list":["post-55747","post","type-post","status-publish","format-standard","hentry","category-server","tag-node-js","tag-87"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Node.js \u4e2d\u7684 Gzip \u538b\u7f29\uff1a\u52a0\u901f\u4f60\u7684 Web \u5e94\u7528\u4f20\u8f93 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/55747.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Node.js \u4e2d\u7684 Gzip \u538b\u7f29\uff1a\u52a0\u901f\u4f60\u7684 Web \u5e94\u7528\u4f20\u8f93 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e49\u6b21\uff0c\u6536\u85cf15\u6b21\u3002Gzip \u538b\u7f29\u662f\u63d0\u5347 Node.js Web \u5e94\u7528\u6027\u80fd\u6700\u7b80\u5355\u6709\u6548\u7684\u624b\u6bb5\u4e4b\u4e00\u3002\u901a\u8fc7zlib\u6a21\u5757\u6216\u4e2d\u95f4\u4ef6\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u54cd\u5e94\u538b\u7f29\uff0c\u663e\u8457\u51cf\u5c11\u4f20\u8f93\u65f6\u95f4\u3001\u8282\u7701\u5e26\u5bbd\u5e76\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002\u4f18\u5148\u538b\u7f29\u6587\u672c\u8d44\u6e90\u3002\u9009\u62e9\u5408\u9002\u7684\u538b\u7f29\u7ea7\u522b\u548c\u9608\u503c\u3002\u5229\u7528\u7f13\u5b58\u907f\u514d\u91cd\u590d\u538b\u7f29\u3002\u751f\u4ea7\u73af\u5883\u8003\u8651\u7528\u53cd\u5411\u4ee3\u7406\u5904\u7406\u538b\u7f29\u3002\u89e3\u538b\u65f6\u6ce8\u610f\u5b89\u5168\uff0c\u9632\u8303\u89e3\u538b\u70b8\u5f39\u3002\u5173\u6ce8 Brotli \u4f5c\u4e3a\u66f4\u9ad8\u6548\u7684\u66ff\u4ee3\u65b9\u6848\u3002\u7acb\u5373\u4e3a\u4f60\u7684 Node.js \u5e94\u7528\u542f\u7528 Gzip\uff0c\u8ba9\u4f60\u7684\u7528\u6237\u4eab\u53d7\u98de\u4e00\u822c\u7684\u52a0\u8f7d\u901f\u5ea6\uff01\ud83d\ude80\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/55747.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-13T23:44:42+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/55747.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/55747.html\",\"name\":\"Node.js \u4e2d\u7684 Gzip \u538b\u7f29\uff1a\u52a0\u901f\u4f60\u7684 Web \u5e94\u7528\u4f20\u8f93 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-13T23:44:42+00:00\",\"dateModified\":\"2025-08-13T23:44:42+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/55747.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/55747.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/55747.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Node.js \u4e2d\u7684 Gzip \u538b\u7f29\uff1a\u52a0\u901f\u4f60\u7684 Web \u5e94\u7528\u4f20\u8f93\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Node.js \u4e2d\u7684 Gzip \u538b\u7f29\uff1a\u52a0\u901f\u4f60\u7684 Web \u5e94\u7528\u4f20\u8f93 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/55747.html","og_locale":"zh_CN","og_type":"article","og_title":"Node.js \u4e2d\u7684 Gzip \u538b\u7f29\uff1a\u52a0\u901f\u4f60\u7684 Web \u5e94\u7528\u4f20\u8f93 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e49\u6b21\uff0c\u6536\u85cf15\u6b21\u3002Gzip \u538b\u7f29\u662f\u63d0\u5347 Node.js Web \u5e94\u7528\u6027\u80fd\u6700\u7b80\u5355\u6709\u6548\u7684\u624b\u6bb5\u4e4b\u4e00\u3002\u901a\u8fc7zlib\u6a21\u5757\u6216\u4e2d\u95f4\u4ef6\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u54cd\u5e94\u538b\u7f29\uff0c\u663e\u8457\u51cf\u5c11\u4f20\u8f93\u65f6\u95f4\u3001\u8282\u7701\u5e26\u5bbd\u5e76\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002\u4f18\u5148\u538b\u7f29\u6587\u672c\u8d44\u6e90\u3002\u9009\u62e9\u5408\u9002\u7684\u538b\u7f29\u7ea7\u522b\u548c\u9608\u503c\u3002\u5229\u7528\u7f13\u5b58\u907f\u514d\u91cd\u590d\u538b\u7f29\u3002\u751f\u4ea7\u73af\u5883\u8003\u8651\u7528\u53cd\u5411\u4ee3\u7406\u5904\u7406\u538b\u7f29\u3002\u89e3\u538b\u65f6\u6ce8\u610f\u5b89\u5168\uff0c\u9632\u8303\u89e3\u538b\u70b8\u5f39\u3002\u5173\u6ce8 Brotli \u4f5c\u4e3a\u66f4\u9ad8\u6548\u7684\u66ff\u4ee3\u65b9\u6848\u3002\u7acb\u5373\u4e3a\u4f60\u7684 Node.js \u5e94\u7528\u542f\u7528 Gzip\uff0c\u8ba9\u4f60\u7684\u7528\u6237\u4eab\u53d7\u98de\u4e00\u822c\u7684\u52a0\u8f7d\u901f\u5ea6\uff01\ud83d\ude80\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/55747.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-13T23:44:42+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/55747.html","url":"https:\/\/www.wsisp.com\/helps\/55747.html","name":"Node.js \u4e2d\u7684 Gzip \u538b\u7f29\uff1a\u52a0\u901f\u4f60\u7684 Web \u5e94\u7528\u4f20\u8f93 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-13T23:44:42+00:00","dateModified":"2025-08-13T23:44:42+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/55747.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/55747.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/55747.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Node.js \u4e2d\u7684 Gzip \u538b\u7f29\uff1a\u52a0\u901f\u4f60\u7684 Web \u5e94\u7528\u4f20\u8f93"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/55747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=55747"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/55747\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=55747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=55747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=55747"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=55747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}