Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Building Scalable Apps with Redis and Node.js
  • Table Of Contents Toc
Building Scalable Apps with Redis and Node.js

Building Scalable Apps with Redis and Node.js

By : Johanan
4.3 (9)
close
close
Building Scalable Apps with Redis and Node.js

Building Scalable Apps with Redis and Node.js

4.3 (9)
By: Johanan

Overview of this book

If the phrase scalability sounds alien to you, then this is an ideal book for you. You will not need much Node.js experience as each framework is demonstrated in a way that requires no previous knowledge of the framework. You will be building scalable Node.js applications in no time! Knowledge of JavaScript is required.
Table of Contents (12 chapters)
close
close
11
Index

Node.js and Node Package Manager

Node.js is a platform that uses Google Chrome's JavaScript engine. This means that we can create server applications using JavaScript. We do not need an in-depth knowledge of everything on Node.js, we only need it to be installed. Node.js binaries are packaged for all the major platforms at http://nodejs.org/download/.

We will use npm (also known as Node Package Manager) to install all the libraries that we are going to use. Most languages/platforms/frameworks move to a managed packaging system. This will be familiar if you have ever used Python's pip with virtualenv, Debian's apt-get, or Microsoft's NuGet, to name a few. Managed packaging allows developers to explicitly define what dependencies your application requires. We will install almost all the Node packages locally. This allows us to install and test new dependencies separately without creating conflicts on the system. By default, npm installs the packages to a folder named node_modules in the root of our project. When a package is used in a file, Node will check this directory for the package. The code that accompanies each chapter will not have the node_modules directory included, but it will have the file that defines what is needed. When we install something globally, we will use the –g flag for npm. This installs the packages to a central node_modules directory so that every Node project can use the package. If you have built a Node.js project before, this should not be new. I will write out the commands for anyone that has not used npm.

Node packages are very notorious for having fast release cycles, which means that by the time you have read this, some of the packages you will use might be of a different version. There are a few ways to combat this. We can use npm shrinkwrap, which will explicitly define each package and all its dependencies. Another way, is to include all the dependencies into source control, so we are completely sure what package and version is installed. I will list out the versions I have used, so that you can install the same versions. As an example, Express has already gone through a major version upgrade (from Version 3.x to 4.x), which was not completely backwards compatible.

The following are the versions that we will use:

  • body-parser: 1.4.3
  • connect: 3.0.2
  • cookie-parser: 1.3.2
  • csurf: 1.3.0
  • ejs: 0.8.5
  • express: 4.6.1
  • express-partials: 0.2.0
  • express-session: 1.6.5
  • redis: 0.10.1
  • connect-redis: 1.4.7
  • connect-flash: 0.1.1

To ensure that these are the versions installed, you can create a file in the root of the project named package.json. The file should look similar to the following code:

{
  "name": "NodeChat",
  "version": "0.0.0",
  "main": "app.js",
  "scripts": {
    "stop": "echo not implemented",
    "start": "node ./app.js"
  },
  "dependencies": {
    "ejs": "0.8.5",
    "express": "4.6.1",
    "express-partials": "0.2.0",
    "redis": "0.10.1",
    "connect-redis": "1.4.7",
    "connect-flash": "0.1.1"
  }
}

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

The package.json file defines attributes about our project using JSON. The key property that we are concerned with here is dependencies. We define each one in the dependency object along with the version. Note that each dependency is pinned to a specific version. We can add a line to install Express 4 or above as follows:

"express": ">=4.0.0"

The issue with this is that we will not know what version we can get. Always prefer explicit dependencies to implicit ones.

We can then install the required packages by running the following command:

npm install
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Building Scalable Apps with Redis and Node.js
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon