转载自: http://inspiredjw.com/do-not-forget-to-use-gzip-for-express/
When a gzip compatible browser requests to a web server, web server can compress the response to the browser back and the browser can decompress the response and finally the browser get the original response.
If the server does not take care of gzip compression, the original size of data is passed which takes longer time than using gzip because it is sending bigger data!
For Express 2.x, I used to use Gzippo which is no longer working for recent version of Node.js and Express.
Since Express 3.x, the express.compress()
is available with Express itself.
How to apply Gzip?
In your main file something like app.js
or index.js
, you have bunch of app.use
.
app.use(express.json());
app.use(express.urlencoded());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
Add express.compress()
at the very top would enable the gzip compression on your web server.
app.use(express.compress());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
For Express 4.x
You need to install middlewares separately for Express 4.x.
var compress = require('compression');
app.use(compress());
Check your website
After applying gzip on your web server, you should check your website whether it is using gzip or not.
Go to GzipTest and enter your site url.
If you see this, you are successfully applied gzip compression on your website!
相关推荐
**Express.js 3入门实例详解** Express.js 是一个基于 Node.js 的开源 web 应用框架,它被广泛用于构建 RESTful API 和 web 应用。Express.js 3 是该框架的一个较早版本,但它的核心概念和设计模式对理解 Express....
**Node.js-Polka:小巧而高效的Express.js替代方案** 在Node.js的Web开发领域,Express.js一直是构建服务器端应用程序的首选框架,以其强大的功能和灵活性而受到广泛的欢迎。然而,随着项目规模的减小和对性能追求...
4. **静态文件服务**:通过`app.use(express.static('public'))`,我们可以轻松地设置一个目录来服务静态资源,如CSS、JavaScript文件和图片。 接下来,我们关注一下Express.js与MySQL的整合: 1. **安装连接库**...
To get the most out of this book, you'll need to know the basics of web application design and be proficient with JavaScript. About the Author Evan Hahn is an active member of the Node and Express ...
Designed for use by novice computer users, this text begins with the basics, such as starting SPSS, defining variables, and entering and saving data. All major statistical techniques covered in ...
博客是互联网上分享个人见解、经验或专业知识的常见平台,"blog-express.rar" 这个压缩包很可能包含了建立一个基于Express.js的博客系统所需的所有资源。Express.js是Node.js框架,它为开发者提供了构建web应用的...
Express.js作为Node.js中最流行的Web应用框架,提供了丰富的扩展性,让我们能够轻松定制自己的访问日志记录器。"Node.js-基于express的访问日志记录器"项目就是这样一个实现,它专注于在Express应用中记录HTTP请求的...
在"JavaScript_解析服务器为Nodejs Express.zip"这个压缩包中,重点是讲解如何利用Node.js的Express框架来搭建一个功能强大的后端服务器。 Express是Node.js中最受欢迎的Web应用框架,它简化了HTTP服务器的创建,...
使用 `app.use(express.static('public'))` 可以设置一个目录作为静态资源服务器,提供 HTML、CSS、JavaScript 等文件。 5. **视图和模板引擎** Express 支持多种模板引擎,如 EJS、Pug、Jade 等。通过 `app.set...
### Express GET 请求详解 在现代Web开发中,Node.js已成为构建高效、可扩展的网络应用和服务端解决方案的重要工具之一。Express作为Node.js平台最流行的框架,简化了许多HTTP操作,包括处理GET请求。本文将深入...
I explain new and interesting bits of syntax, but this book isn’t primarily about teaching you how to use JavaScript. It’s about teaching you how to build real applications by using AdonisJs. The ...
You will begin with a basic RESTful web service in Node.js, using the popular Express.js framework, pre-built and ready to run in your local environment. You will be introduced to the most powerful ...
在本项目中,我们探讨的是如何使用Node.js和Express.js框架构建一个简单的待办事项(To-Do List)应用程序。Express.js是Node.js生态系统中最受欢迎的Web应用框架,它简化了构建RESTful API和Web应用的过程。让我们...
Learn to use Node.js by creating a fully functional social network About This Book Plan and implement a modern Node.js application Get to know the most useful Node.js capabilities Learn how to create...
Express是Node.js中的一款广泛应用的Web开发框架,它简化了构建高效、可扩展的网络应用的过程。本项目“express.zip”旨在模拟一个基于Express的基本Web应用程序。以下将详细阐述Express框架的核心特性、如何使用它...