1

Create a web application in Node and Express framework using Express Generator

[Social9_Share type="horizontal"]

Create a web application in Node and Express framework using Express Generator

In this post, let us learn how to create a web application with NODE and Express JS using express generator. It is easy to create a web application using express generator, as it gives the complete template of your project.

Prerequisites

Let me write down the steps to create a web application in Node JS and Express4.0 using Express Generator

  • Install express generator
  • Use express command to create the project
  • Use npm install to install the dependencies
  • Modify app.js if needed
  • Run the application using the command SET DEBUG
  • View the output in the browser

Let’s get started.
First, open command line and install express-generator as a global module.

This module gets installed globally in C:\Users\{user-name}\npm\node_modules. With the help of globally installed generator we will be creating the project.The screenshot is shown below.

exgen-install

Next, select any folder of your choice to create the node project. Navigate to that folder in command line and then type

If you want to use the template engine as Jade, then you can use the same command. But if you want to use the template engine as EJS, then the command for creating the project using the generator is

Since, I am using ejs as the template engine I prefer the second command. The screenshot is given below.

exgen-create

You can see that the project is created with series of directories as javascripts, stylesheets, routes, views and with files as package.json, app.js and few other files. This also specifies the next step to be executed (the command to install the dependencies and the command to run the application).

The project structure of the Node application that is created using express-generator.

exgen-struc

Lets view the package.json file that is created. You can see all the dependencies needed for the project is added here automatically.

exgen-json

Next, to install the dependencies, move to command line and type

or navigate to the project directory and type npm install.

exgen-depen

A directory with the name node_modules is created in the project folder and all the dependencies are added automatically in node_modules directory. Now your project setup is ready.

Let us open app.js and review it. All the dependencies are invoked by the use of require method. The screen shot of app.js is given below.

exgen-app

The use of these modules:

body-parser – A middleware for handling Raw, Text, JSON and URL encoded form data(especially POST data).
Cookie-Parser – To parse Cookie header and populate req.cookies with an object keyed by the cookie names.
EJS -A template engine . It combines data and the template to produce HTML.
express - A web application framework that provides a set of features for web and mobile applications
morgan – A HTTP request logger middleware for node.js

Now, let’s run our application. To run you need to use the command given below

exgen-run

Go to command line and type this.

exgen-run1

The server gets started and listens to port 3000. Open the browser and loadhttp://localhost:3000.You will get the output as shown below.

exgen-output

So, with the help of express-generator, you have created a web application of Node js and Express easily. Now, you can modify this application in whichever way you want.

In the next post, let us discuss about routing in Express Framework

Happy Learning

Post a comment