Express JS and Express Generator Module Basics, Express JS Sample WebApplication

Filed Under: Node.js

As we discussed in my initial Node JS Platform posts, Node JS has many modules to develop different kinds of functionality. Each Node JS Module is aimed to provide one or more kinds of services. Like other modules or packages, Express JS is also a module in Node JS Platform.

In this post, we will discuss some basics of “Node: Express JS Module”. As it is very big and important module in Node JS Platform, we will discuss some advanced concepts of Express JS module in coming posts.

What is Express JS

Express JS is a Light-weight Web Framework to develop Web Applications very easily and quickly in Node JS Platform. Latest Express JS Framework version is : 4.x.

Express JS Official Website: http://expressjs.com/

In my previous post “Node JS HTTP Module”, we have developed an HTTP Server using Node JS “http” module. We can develop similar kind of HTTP Server or WebServer using Express JS Module too.
We will discuss on “How to develop a WebServer using Express JS Module” in coming posts.

Express JS Features

Express JS Framework provides the following features

  • Light-weight Web Application Framework
  • It Supports Routings
  • It supports Template Engines
  • It supports File Uploading
  • Develop SPA(Singe Page WebApplications)
  • Develop Real-time Applications

NOTE:-
Node JS: HTTP Module is basic module to develop HTTP based Networking applications. Node JS: Connect Module is developed on top of HTTP module to provide some extract features like Cookies, Middleware,CSRF etc. Node JS: Express module is developed on top of “Connect” module to provide some more additional features on top of HTTP and Connect Modules.

Express JS Setup

Unlike some Node JS Default Modules like “npm”,”http”,”events” come with Node JS Platform basic installation (No need of separate steps to install these modules), Express JS does not come with as Node JS Default modules. We need to install it manually.

We have already discussed on “How to install a Node JS Module” using “node install” command. If you are not familiar about this command, please go through my previous post : “NPM install update and uninstall example”

To install Express JS globally
Before staring Express JS Development, first and fore most step We need to do is use “npm install” command to install Express JS module


 npm install -g express

Here “express” means Express JS Module and “-g” means install Express JS Module globally.

Open command prompt and execute above command to install Express JS Module globally.

expressjs_install

To verify installation

After Express JS installation is done, we need to check whether this module is installed successfully or not. If it is installed successfully, we can find a new folder at
C:\Users\[Windows_USerName]\AppData\Roaming\npm\node_modules\express

expressjs_install_success

Here we are able to see “express” folder under “node_modules” folder. That means our Express JS is installed successfully.

Node JS Platform has another module : “express-generator”. Earlier “express-generator” module is part of “express” module only. But to provide clear modularity, they have separated into two modules.

What is Express Generator

Like Express JS, Express Generator is also a Node JS Module. It is used to quick start and develop Express JS applications very easily. It does not come as part of Node JS Platform basic installation. We need to install it manually.

To install Express JS globally
Open command prompt and execute this command: npm install -g express-generator

express_generator_install

To verify installation
After Express Generator installation is done, we need to check whether this module is installed successfully or not.

express_generator_install_success

Here we are able to see “express-generator” folder under “node_modules” folder. That means our Express Generator is installed successfully.

Express JS Simple WebApplication Example

We are going to develop simple Express JS WebApplication using Express Generator Module. We are really NOT developing this application from the scratch, just taking Express Generator help.

Please follow these steps to develop and run simple Express JS WebApplication:

  • First install both Node JS Modules: “express” and “express-generator”. We have already done it.
  • Open command prompt in our local FileSystem and execute the “express” command.
  • “express” command syntax:

    
        express <Your-ExpressJS-Application-Name>
        

    Example:

    
        express ExpressSampleWebApp
        

    express_sample_webapp

    If we observe this screenshot, “express” command has downloaded a sample Express JS WebApplication with given Application name. It also displays two instructions at the end of the log.

    1. cd ExpressSampleWebApp && npm install
    2. It instructs us use “cd” command to move to our application folder and build our application by executing “npm install” command

    3. SET DEBUG=ExpressSampleWebApp:* & npm start
    4. It instructs us set debut mode if required and execute “npm start” command to start our application.

    Now if we access our application folder , we can see the following content.

    express_sample_webapp_content

  • Execute above two commands one by one from command prompt
  • To install (build) our application:

    
    npm install
    

    express_sample_webapp_install

    To start our Express application:

    
    npm start
    

    express_sample_webapp_start

  • Access Express JS Sample WebApplication using http://localhost:3000/ from our browser.
  • express_sample_webapp_output

    When we execute “npm start” command, by default it starts Express JS WebServer at 3000 port number and deploy our Express JS Application into server.

It is very easy right to develop a simple Express JS WebApplication using Express Generator module. From this point, we can add our required components and code to improve this application functionality.

Here we have developed just sample Express Application without using any Tools or IDEs. In my upcoming posts, we will discuss on “How to develop Express JS Applications using Eclipse Enide 2014 IDE”.

That’s it about Express JS Basics. If you have queries, please drop me a comment.

Comments

  1. Ramzan says:

    Very superb article sir . I salute you

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages