Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Compressing object-body content from a Node.js server built using the express module


If space is your primary concern when using JSON that has you considering a binary representation, you should seriously consider using compression instead. Compression can yield similar savings to a binary representation, it is implemented with gzip in most servers and HTTP clients, and can be added as a transparent layer after you've finished debugging your application. Here, we discuss adding object-body compression for JSON and other objects sent by the popular express server built on top of Node.js with the express module.

Getting ready

First, you need to make sure you've installed the express and compress modules:

npm install express
npm install compression

You could also npm install –g it, if you want it to be available to all Node.js applications in your workspace.

How to do it…

When initializing your express module in your server's entry point, require compression, and tell express to use it:

var express...