Create your first Node.js app using Express Framework


nodejs_logo_green

What is Node.js?

Node.js is a server side platform built on Google Chrome’s JavaScript Engine(V8 Engine). Node.js was developed by Ryan Dahl in 2009 and it’s latest version is v0.12.7. Definition of Node.js as put by its official documentation is as follows:

Node.js® is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Node.js is an open source, cross-platform Runtime Environment for server-side and networking applications. Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows, and Linux.

Download and install Node.js from the following link:   https://nodejs.org/download/


 

Node.js – npm

Node Package Manager (npm) provides following two main functionalities:

  1. Online repositories for node.js packages/modules which are searchable on search.nodejs.org
  2. Command line utility to install Node.js packages, do version management and dependency management of Node.js packages.

npm comes bundled with Node.js installables after v0.6.3 version. To verify the same, open console and type following command and see the result:

$ npm -v


 

Node.js – Express Framework

Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications. It facilitates a rapid development of Node based Web applications.

Installing Express

Firstly, install the Express framework globally using npm so that it can be used to create web application using node terminal.

$ sudo npm install -g express-generator


Create your first express application

Step 1:Type express <app_name>  in command line to create to your first app.This will create a new folder with name app_name in the current directory.

$ express sample

Screen Shot 2015-09-02 at 12.43.39 pm

 

Step 2:Install the dependencies. These are the packages that are associated with the app. Jade is used to create the front end by default. npm install is used to install all the dependencies and is saved in the packages folder in your application folder.

$cd sample

$npm install

Step 3:Run the app.When you run the app you will get a message “sample:server Listening on port 3000 +0ms” on the command line.Now open the browser and go to localhost/3000 to see the app.

$ DEBUG=sample:* npm start

Screen Shot 2015-09-02 at 1.11.17 pm