{"id":7955,"date":"2020-02-04T20:52:03","date_gmt":"2020-02-04T20:52:03","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7955"},"modified":"2020-02-04T20:52:03","modified_gmt":"2020-02-04T20:52:03","slug":"express-server-and-compression","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/express-server-and-compression\/","title":{"rendered":"Express server and compression"},"content":{"rendered":"<p>You may want to turn on compression within your express service(s) in which case simply run<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nyarn add compression\r\n<\/pre>\n<p>and we just need to add to our server code like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport express from &quot;express&quot;;\r\nimport compression from &quot;compression&quot;;\r\n \r\nconst app = express();\r\nconst server = http.createServer(app);\r\n \r\napp.use(compression());\r\n \r\nconst port = 4000;\r\nserver.listen(port, \r\n  () =&gt; console.log(`Server on port ${port}`)\r\n);\r\n<\/pre>\n<p>Now, in many case you&#8217;ll possibly already have the client header including the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nAccept-Encoding: gzip, compress, br\r\n<\/pre>\n<p>or you can set the headers yourself and express will serve up gzip compressed responses. <\/p>\n<p>Remember though, that compression happens based upon reading an amount of data before starting to stream\/send the response, so small(ish) amounts of data will not benefit from any performance increases, whereas large amounts of data will by the very nature of compression, reduce the amount of data being transferred and therefore you should see a performance increase.<\/p>\n<p>See also <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Compression\" rel=\"noopener noreferrer\" target=\"_blank\">Compression in HTTP<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may want to turn on compression within your express service(s) in which case simply run yarn add compression and we just need to add to our server code like this import express from &quot;express&quot;; import compression from &quot;compression&quot;; const app = express(); const server = http.createServer(app); app.use(compression()); const port = 4000; server.listen(port, () =&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[268],"tags":[],"class_list":["post-7955","post","type-post","status-publish","format-standard","hentry","category-express"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7955"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7955\/revisions"}],"predecessor-version":[{"id":7958,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7955\/revisions\/7958"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}