Creating Node.js Web App With Express
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
bash > nodejs -v v0.10.12 bash > npm -v 1.2.32
Version number will vary based on when you install the packages
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
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.