@loveagileqa
qualityworkscg.com
express
&
Node.js is a server side JavaScript execution environment. It’s a platform built on Google Chrome’s V8 JavaScript runtime. It helps in building highly scalable and concurrent applications rapidly.
The beauty of Node is its non-blocking I/O nature which allows it to be very fast and utilize resources very efficiently. Node in conjunction with mainly libraries can create a wide array of applications from chat servers to remote controls!!
Express.js is lightweight framework used to build web applications in Node. It provides a number of robust features for building single and multi page web application. It basically sits on top of Node to provide developers with tools to utilize the features of Node to make writing applications faster, easier and more fun :)
Introduction
Prerequisites
Before installing node (Mac)
To compile node we need gcc, make and git to import node source code.
$ brew install git
Clone Node's Git repository
Cloning node.js source code:
$ git clone git://github.com/joyent/node.git
Compile & install Node.js
Go into the newly created node folder and install node
$ cd node
$ git checkout v0.12.4
$ ./configure
$ make
$ sudo make install
<-- recommended version
Add user´s directory to BIN Paths
Open the sudoers file
$ sudo su
$ nano /etc/sudoers
Inside the editor scroll to where you see the line:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Append :/usr/local/bin to the end so it should appear like:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
Install npm
Download and install npm
$ git clone https://github.com/isaacs/npm.git
$ cd npm
$ sudo make install
npm stands for Node Package Manager and is useful tool when developing in node to easily manage packages and dependencies for a node project
Testing Node & npm
Test that node and npm are installed
$ node -v
$ npm -v
Installing Express
Install express and express-generator globally
$ npm install -g express
$ npm install -g express-generator
express-generator, as the name says, will generate an express application's skeleton for us with some boiler-plate code.
Prerequisites
Your first app
(Yes it will be a Hello World app)
Create node/express projects folder
Create the project's folder and go into it.
$ mkdir node-express
$ cd node-express
Create the express project
Run the express command to create an express project.
$ express hello-world
$ cd hello-world
This will create a folder called hello-world that contains the basic components of an express project.
The components of an express project
Installing project dependencies
Once the project has been created we now need to install the dependencies.
$ npm install
This will create a node_modules folder that will house all of our local project packages, based on our package.json file.
Understanding the app.js file
The app.js file is the heart of our application and can be broken down into various sections.
The first section defines some variables based on installed packages and even sets some routes based on ones given to us by express. In the last line we instantiate express and assign i to an app variable.
Understanding the app.js file
The next section tells the app where to find views what engine to use to render them and calls some methods to get the app prepared to run effectively. It also defines a few routes and tells the app what files to use for each. We recommend visiting the Express site to learn more about these methods.
Understanding the app.js file
The next section focuses on error handlers for development and production telling the app to print out error information when in development mode. We are not too worried about these for now.
Understanding the app.js file
The last line exports our app variable so it can be called and used in other files.
Writing our hello world code
Navigate to our routes file and open the index.js file.
Lets change our title variable to 'Hello World' and then lets open our index.jade file in our views folder.
Writing our hello world code
This file is written in Jade and calls our layout page as a type of master page.
Lets change our line 5 to read 'p Welcome <your name>'.
Running & viewing our application
Before we can run our application, we must tell it which port to listen on. Add the following lines to the end of your app.js file.
Finally, we can run our app by navigating to our project's folder (hello-world) and running the following command.
$ node app.js
Running & viewing our application
Your console should look like this.
We can now navigate to our browser and view our application at localhost:3000.
Your first app
Our App
The QualityWorks Consulting Group Jamaican team has created a sample RESTful app for you to test.
Features
Accessible at: GitHub
After downloading the app you can get started with running it and seeing what it can do. If you are not sure of how Mongo works, check out our Mongo DB tutorial.
In our Testing tutorial, we'll show you how to perform automated tests on the app.
The app provides code for local and online database connection. Simply configure which ever one you plan to use in the app.js file and use the live or local variable in the Mongo DB connection string depending on your settings.
Congratulations
Check out our next tutorial