5 Easy Performance Tweaks for Node.js Express
Node.js has revolutionized web development and the Express framework must share much of the credit. Express may not be the fastest or most advanced server option, but it’s almost certainly the most-used with more than 3 million downloads per month.
If you do nothing, Node.js and Express run blazingly fast. However, there are a number of simple ways to make Express 4.x run even faster…
1. Switch to Production Mode
Express can run in several modes. By default, it presumes development mode which provides exception stack traces and other logging tasks. There is also a debugging mode which logs messages to the console, e.g.
[code]
DEBUG=express:* node ./app.js
[/code]
On your live server, you can noticeably improve performance by switching to production mode. This is achieved by setting the NODE_ENV
environment variable to production
. It can be set in the current session on Windows prior to launching your Express application:
[code]
set NODE_ENV=production
[/code]
or Mac/Linux:
[code]
export NODE_ENV=production
[/code]
Linux users can also set NODE_ENV
in a single line when launching:
[code]
NODE_ENV=production node ./app.js
[/code]
Ideally, you should configure your environment by adding export NODE_ENV=production
to your ~/.bash_profile
or appropriate start-up script.
2. Enable GZIP
Express 4.x provides minimal functionality which can be enhanced with middleware. One of the less obvious missing features is GZIP compression which shrinks the HTTP payload so it can be expanded by the browser on receipt. To add GZIP, install the compression module using npm:
[code]
npm install compression –save
[/code]
(Depending on your setup, you may need to use sudo
on Mac/Linux — there are various ways to fix that.)
In your main application launch file, include the compression module:
[code language=”javascript”]
var compression = require(‘compression’);
[/code]
then mount it as the first middleware function (prior to other Express .use
methods):
[code language=”javascript”]
// GZIP all assets
app.use(compression());
[/code]
Continue reading %5 Easy Performance Tweaks for Node.js Express%
LEAVE A REPLY
You must be logged in to post a comment.