Note : This article is updated for Latest release of Express 4.0
This article shows the creation of Node.js app using Express web framework on Debian based system.
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
$ > nodejs -v v0.10.12 $ > npm -v 1.2.32
Version number will vary based on when you install the packages
For Express 3.x,
Now, installation of the express is easy with npm as shown in following command,
sudo npm install -g express@3
Note that, in above particularly, in ‘express@3′. the no. after @ indicates the major version of express to be installed. which for us 3.x
This installs express 3.x node module along with its CLI globally ( -g switch) meaning that it can be available from anywhere.
Now, check for successful installation.
$ > express -V (note ‘v’ in caps) 3.9.0
For Lastest Express 4.x,
sudo npm install -g express
Express 4.x has significant changes in it, one them is that it does not has ‘express’ binary cli by default installed as core ‘core express npm module’.
It is separated in another npm module, called ‘express-generator’, which we can install using as follows-
sudo npm install -g express-generator
Now, we have express module installed as globally, we can check for successful installation as follows-
$ > express -V (note ‘v’ in caps) 4.4.0
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
$ > 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
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
If you installed express version 3.x then run the application using following command
$ > node app Express server listening on port 3000
or second option as -
$ > npm start > email@example.com start /home/siddhi/workspace/exp3 > node app.js Express server listening on port 3000
For Express 4.x, you need to start application using second option.
Now, we start our web application, lets open the browser hit the URL. http://localhost:3000
You will see something like following in browser
Congratulations! You just created the first NodeJS Web app with Express web framework.