HTML5 Zone is brought to you in partnership with:

Abhijeet is a DZone MVB and is not an employee of DZone and has posted 4 posts at DZone. You can read more from them at their website. View Full User Profile

Creating Node.js Web App With Express

01.23.2014
| 9444 views |
  • submit to reddit

Introduction

This article shows the creation of Node.js app using Express web framework on Debian based system.

Node.js is platform for building server side using the event-driven i/o with JavaScript language. NPM is Node Package Manager, it is for handling the dependencies for node applications. I assume basic understanding of NodeJS and NPM for this article.

Express is web development framework for Node providing the lot of features.

Following are features of Express

  • Built based on Connect and inspired by Sinatra in Ruby world
  • Command line interface (CLI) support
  • Templating and view rendering with jade and many others
  • Dynamic CSS support with LESS and stylus
  • Robust Routing
  • Environment based configuration
  • Session Management
  • Logging in applications

Installation of packages

Following are the installation instructions based on the Ubuntu or any Debian based system. Before starting with Express installation, you need to install the Node and NPM.

Node and NPM

For installation of the latest version of Node package on Ubuntu or any Debian based system, there exists package from Chris Lea  from his PPA (personal package archive) repo which includes installation both latest Node and NPM.

Lets go ahead and execute the following set of commands on bash

sudo apt-get update
sudo apt-get install python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update

After that  lets install the NodeJS

sudo apt-get install nodejs

To check for correct installation, check for the version number on the each of them. Hit the following commands on the bash for same

bash > nodejs -v
v0.10.12
bash > npm -v
1.2.32

Version number will vary based on when you install the packages

Express

Now, installation of the express is easy with npm as shown in following command,

sudo npm install -g express

This installs express node packages along with its CLI globally ( -g switch) meaning that it can be available from anywhere.

Now, check for successful installation.

bash> express -V (note ‘v’ in caps)
3.3.1

Creation of application using express CLI

After successful installation, you will have express CLI in form of express command itself.

Now, if you do express with  help switch (-h), it lists out the possible usage of express command

bash > express -h
Usage: express [options]
Options:
    -h, --help       output usage information<
    -V, --version    output the version number
    -s, --sessions   add session support
    -e, --ejs        add ejs engine support (defaults to jade)
    -J, --jshtml     add jshtml engine support (defaults to jade)
    -H, --hogan      add hogan.js engine support
    -c, --css <engine>  add stylesheet <engine> support (less|stylus) (defaults to plain css)
    -f, --force      force on non-empty directory

Lets create the Express default project using express command by passing the name of project, as shown in follows

bash> express helloexpress
    create : helloexpress
    create : helloexpress/package.json
    create : helloexpress/app.js
    create : helloexpress/public
    create : helloexpress/public/javascripts
    create : helloexpress/public/images
    create : helloexpress/public/stylesheets
    create : helloexpress/public/stylesheets/style.css
    create : helloexpress/routes
    create : helloexpress/routes/index.js
    create : helloexpress/routes/user.js
    create : helloexpress/views
    create : helloexpress/views/layout.jade
    create : helloexpress/views/index.jade
   install dependencies:
       $ cd helloexpress && npm install
   run the app:
      $ node app

It creates the some directories and files in the directory named helloexpress.

Change directory to helloexpress and install the dependencies with npm,  as shown in follows

cd  helloexpress
sudo npm install

Above commands creates the default Express project with server ready to listen onto port 3000.

Now, lets start the server with following

bash > node app
Express server listening on port 3000

Open the browser hit URL  http://localhost:3000

You will see something like following in browser

Congratulations! You just created the first NodeJS app with Express web framework.

In the next article, i will discuss some internals of this reference to this app in accordance to Express.

Published at DZone with permission of Abhijeet Sutar, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)