Node - Js Essentials - Sample Chapter
Node - Js Essentials - Sample Chapter
Node.js Essentials
Node.js Essentials
Node.js Essentials takes you through the fundamentals
of Node.JS and demonstrates how Node.js can transform
the way you work with JavaScript and allow you to take
greater control over your code.
You will also learn how to create a HTTP server from
scratch, route requests, create a RESTful interface, and
authenticate users. You'll nd out how to test and optimize
your code when working with Node.js, using the Mocha
framework. Follow each step and discover how to test
both synchronous and asynchronous code using Mocha
unit tests.
With Node.js Essentials you'll nd everything you
need to create a complete full-stack application with
Node.js. It will help you harness JavaScript much more
effectively and in doing so expand your condence
and capabilities as a developer.
P U B L I S H I N G
pl
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Node.js Essentials
$ 24.99 US
15.99 UK
Sa
m
Fabian Cook
ee
From client to server, learn how Node.js can help you use
JavaScript more effectively to develop faster and more scalable
applications with ease
Fabian Cook
New Zealand. He began working with Java and C# very early in his life, which lead
to using Node.js in an open source context. He is now currently working for a New
Zealand ISP, known as NOW NZ where they are utilizing the full power of Node.js,
Docker and CoreOS.
Preface
Preface
Node.js is simply a tool that lets you use JavaScript on the server side. However, it
actually does much more than that by extending JavaScript, it allows for a much
more integrated and efficient approach to development. It comes as no surprise that
it's a fundamental tool for full-stack JavaScript developers. Whether you work on the
backend or frontend, you adopt a much more collaborative and agile way of working
using Node.js, so that you and your team can focus on delivering a quality end
product. This will ensure that you're ready to take on any new challenge that gets
thrown at you.
This book will be fast paced and cover dependency management, running your own
HTTP server, real time communication, and everything in between that is needed to
get up and running with Node.js.
Preface
Chapter 6, LevelDB and NoSQL, covers the introduction of NoSQL databases, such as
LevelDB and MongoDB. It also covers the use of the simple key/value store and a
more complete document database.
Chapter 7, Socket.IO, explores the real-time communication between clients, servers,
and back again and also how it authenticates and notifies the users.
Chapter 8, Creating and Deploying Packages, focuses on sharing the modules and
contributing to the eco-system
Chapter 9, Unit Testing, tests your code using Mocha, Sinon, and Chance and
also covers how to use mocks with functions and generate random values to
test your code
Chapter 10, Using More Than JavaScript, explains the usage of CoffeeScript with Node.
js to expand language capabilities.
Getting Started
Every Web developer must have come across it every once in a while, even if they
just dabble in simple Web pages. Whenever you want to make your Web page a
little more interactive, you grab your trustworthy friends, such as JavaScript and
jQuery, and hack together something new. You might have developed some exciting
frontend applications using AngularJS or Backbone and want to learn more about
what else you can do with JavaScript.
While testing your website on multiple browsers you must have come across Google
Chrome at some point and you might have noticed that it is a great platform for
JavaScript applications.
Google Chrome and Node.js have something very big in common: they both work on
Google's high-performance V8 JavaScript engine, this gives us the same engine in the
browser that we will be using in the backend, pretty cool, right?
Setting up
In order to get started and use Node.js, we need to download and install Node.js.
The best way to install it will be to head over to https://nodejs.org/ and
download the installer.
At the time of writing, the current version of Node.js is 4.2.1.
To ensure consistency, we are going to use a npm package to install the correct
version of Node.JS and, for this, we are going to use the n package described at
https://www.npmjs.com/package/n.
Currently, this package has support only for *nix machines. For Windows. see
nvm-windows or download the binary for 4.2.1 from https://nodejs.org/dist/
v4.2.1/.
[1]
Getting Started
The g argument will install the package globally so we can use the package anywhere.
Linux users may need to run commands that install global packages as sudo.
Using the recently install package, run:
[~]$ n
If node/4.2.1 isn't marked we can simply run the following packages; this will
ensure that node/4.2.1 gets installed:
[~]$ sudo n 4.2.1
To ensure that the node is good-to-go, lets create and run a simple hello world
example:
[~/src/examples/example-1]$ touch example.js
[~/src/examples/example-1]$ echo "console.log(\"Hello world\")" >
example.js
[~/src/examples/example-1]$ node example.js
Hello World
Hello require
In the preceding example, we just logged a simple message, nothing interesting,
so let's dive a bit deeper in this section.
[2]
Chapter 1
When using multiple scripts in the browser, we usually just include another script
tag such as:
<script type='application/javascript' src='script_a.js'>
</script>
<script type='application/javascript' src='script_b.js'>
</script>
Both these scripts share the same global scope, this usually leads to some unusual
conflicts when people want to give variables the same name.
//script_a.js
function run( ) {
console.log( "I'm running from script_a.js!" );
}
$( run );
//script_b.js
function run( ) {
console.log( "I'm running from script_b.js!" );
}
$( run );
This can lead to confusion, and when many files are minified and crammed together
it causes a problem; script_a declares a global variable, which is then declared
again in script_b and, on running the code, we see the following on the console:
> I'm running from script_b.js!
> I'm running from script_b.js!
The most common method to get around this and to limit the pollution of the global
scope is to wrap our files with an anonymous function, as shown:
//script_a.js
(function( $, undefined ) {
function run( ) {
console.log( "I'm running from script_a.js!" );
}
$( run );
})( jQuery );
//script_b.js
[3]
Getting Started
(function( $, undefined ) {
function run( ) {
console.log( "I'm running from script_b.js!" );
}
$( run );
})( jQuery );
This is good for code that isnt depended upon externally, but what do we do for the
code that is? We just export it, right?
Something similar to the following code will do:
(function( undefined ) {
function Logger(){
}
Logger.prototype.log = function( message /*...*/ ){
console.log.apply( console, arguments );
}
this.Logger = Logger;
})( )
Now, when we run this script, we can access Logger from the global scope:
var logger = new Logger( );
logger.log( "This", "is", "pretty", "cool" )
> This is pretty cool
So now we can share our libraries and everything is good; But what if someone else
already has a library that exposes the same Logger class.
What does node do to solve this issue? Hello require!
Node.js has a simple way to bring in scripts and modules from external sources,
comparable to require in PHP.
Lets create a few files in this structure:
/example-2
/util
index.js
logger.js
[4]
Chapter 1
main.js
/* util/index.js */
var logger = new Logger( )
var util = {
logger: logger
};
/* util/logger.js */
function Logger(){
}
Logger.prototype.log = function( message /*...*/ ){
console.log.apply( console, arguments );
};
/* main.js */
util.logger.log( "This is pretty cool" );
So why is this? Shouldn't they be sharing the same global scope? Well, in Node.
js the story is a bit different. Remember those anonymous functions that we were
wrapping our files in earlier? Node.js wraps our scripts in them automatically and
this is where require fits in.
Lets fix our files, as shown:
/* util/index.js */
Logger = require( "./logger" )
/* main.js */
util = require( "./util" );
[5]
Getting Started
If you notice, I didn't use index.js when requiring util/index.js; the reason for
this is that when you a require a folder rather than a file you can specify an index
file that can represent that folder's code. This can be handy for something such as a
model folder where you expose all your models in one require rather than having a
separate require for each model.
So now, we have required our files. But what do we get back?
[~/src/examples/example-2]$ node
> var util = require( "./util" );
> console.log( util );
{}
Still, there is no logger. We have missed an important step; we haven't told Node.js
what we want to expose in our files.
To expose something in Node.js, we use an object called module.exports. There
is a shorthand reference to it that is just exports. When our file is wrapped in an
anonymous function, both module and exports are passed as a parameter, as shown
in the following example:
function Module( ) {
this.exports = { };
}
function require( file ) {
// .....
returns module.exports;
}
var module = new Module( );
var exports = module.exports;
(function( exports, require, module ) {
exports = "Value a"
module.exports = "Value b"
})( exports, require, module );
console.log( module.exports );
// Value b
[6]
Chapter 1
Running main.js:
[~/src/examples/example-2]$ node main.js
This is pretty cool
Require can also be used to include modules in our code. When requiring
modules, we don't need to use a file path, we just need the name of the node
module that we want.
Node.js includes many prebuilt core modules, one of which is the util module.
You can find details on the util module at https://nodejs.org/api/util.html.
Let's see the util module command:
[~]$ node
> var util = require( "util" )
> util.log( 'This is pretty cool as well' )
01 Jan 00:00:00 - This is pretty cool as well
[7]
Getting Started
Hello npm
Along with internal modules there is also an entire ecosystem of packages; the most
common package manager for Node.js is npm. At the time of writing, there are a total
of 192,875 packages available.
We can use npm to access packages that do many things for us, from routing HTTP
requests to building our projects. You can also browse the packages available at
https://www.npmjs.com/.
Using a package manager you can bring in other modules, which is great as you can
spend more time working on your business logic rather than reinventing the wheel.
Let's download the following package to make our log messages colorful:
[~/src/examples/example-3]$ npm install chalk
On running this code, you will see the first message in a default color and the second
message in blue. Let's look at the command:.
[~/src/examples/example-3]$ node index.js
I am just normal text
I am blue text!
Having the ability to download existing packages comes in handy when you require
something that someone else has already implemented. As we said earlier, there are
many packages out there to choose from.
We need to keep track of these dependencies and there is a simple solution to that:
package.json.
Using package.json we can define things, such as the name of our project, what the
main script is, how to run tests, our dependencies, and so on. You can find a full list
of properties at https://docs.npmjs.com/files/package.json.
[8]
Chapter 1
npm provides a handy command to create these files and it will ask you the relevant
questions needed to create your package.json file:
[~/src/examples/example-3]$ npm init
The preceding utility will walk you through the creation of a package.json file.
It only covers the most common items and tries to guess valid defaults.
Run the npm help json command for definitive documentation on these fields and
to know what they do exactly.
Afterwards, use npm and install <pkg> --save to install a package and save it as a
dependency in the package.json file.
Press ^C to quit at any time:
name: (example-3)
version: (1.0.0)
description:
entry point: (main.js)
test command:
git repository:
keywords:
license: (ISC)
The utility will provide you with default values, so it is easier to just skip through
them using the Enter key.
[9]
Getting Started
Now when installing our package we can use the --save option to save chalk as a
dependency, as shown:
[~/src/examples/example-3]$ npm install --save chalk
Now, all we need to do is run npm start and then npm will run the start script we have
already specified.
[ 10 ]
Chapter 1
We can define more scripts, for example if we want a start script for the development
environment we can also define a development property; with non-standard script
names however, instead of just using npm <script>, we need to use npm run
<script>. For example, if we want to run our new development script we will have
to use npm run development.
npm has scripts that are triggered at different times. We can define a postinstall
script that runs after we run npm install; we can use this if we want to trigger a
You can read more about the scripts object here: https://docs.npmjs.com/misc/
scripts.
You need to define a package if you are working in a team of developers where the
project is to be installed on different machines. If you are using a source control tool
such as git, it is recommended that you add the node_modules directory into your
ignore file, as shown:
[~/examples/example-3]$ echo "node_modules" > .gitignore
[~/examples/example-3]$ cat .gitignore
node_modules
Summary
That was quick, wasn't it? We have covered the fundamentals of Node.js, which we
need to continue on our journey.
We have covered how easy it is to expose and protect public and private code
compared to regular JavaScript code in the browser, where the global scope can
get very polluted.
We also know how to include packages and code from external sources and how to
ensure that the packages included are consistent.
As you can see there is a huge ecosystem of packages in one of the many package
managers, such as npm, just waiting for us to use and consume.
In the next chapter, we will focus on creating a simple server to route, authenticate,
and consume requests.
[ 11 ]
www.PacktPub.com
Stay Connected: