A Simple App with Express And NodeJS

Posted By : Prabjot Singh | 25-Mar-2015

A Simple App with Express and Node js

Express is a NodeJS web framework which provides lot of features like routing,rendering,rest control methods. Here I will cover most important points before building my simple application. The following points are :-

  1. Directory structure

  2. Creating and installing dependencies (such as MySQL, Express)

  3. Configuration of Express server

  4. Routing (how to handle request)

1) Directory Structure

This is the most important part of application. Without understanding of structure of application, we can not understand the routing concept,how to code for handle request and redirect response.

  • First of all,we have to download and install NodeJS in our system. You can download NodeJS

  • Create simple empty project. I am using IDE(webstorm),you can create it by clicking on file>new project

    A pop up window will be open,in window, give your project name,chose project location,

    and project type,for now, you can choose empty project as project type and create it.

  • Create Server.js file ,there you will put server configuration

  • Create Package.json file,there you will put dependencies and your application information

  • Create folder naming Router ,create main.js file inside of it. There you will put routing configuration of your application

  • Create view folder for your html files

Finally your directory structure will looks like

2) Creating and installing dependencies

I hope you have created package.json file. package.json is used for creating your dependencies. So,there before to start ,you have to install Express. So, paste below code in package.json file

 {
"name": "Express-web-app",
"version": "1.0.0",
"dependencies":
{
"express": "~4.0.0",
"ejs": "~1.0.0"
}
}

 

Then open terminal, switch to thar folder where you have created pacakge.json file and then type command “npm install” . Dependencies will be download and install. So after it,node_modules folder will be created in your application and inside it you can see your dependencies folder

There are some notations in dependencies,which on you have to focus

  • “~” it means that version of dependency or above that version,not below

  • “*” any version

3) Configuration Your Express Server

So,you have installed your required dependencies for project. Move on next step,there we will configure our express server. No more configuration here,only you have to paste below code in your Server.js file

 

 var express=require('express');
var app=express();
require('./router/main')(app);
app.set('views',__dirname + '/views');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
var server=app.listen(3000,function(){
console.log("Express is running on port 3000");
});
 

save your file and do not run code now.

4) Routing (handle client request)

Again,routing is important part of application. Routing tells how to handle client request,how to response of request. Paste below code for basic routing of application

 

 module.exports=function(app)
{
app.get('/',function(req,res){
res.render('index.html')
});
}
 

We can do routing configuration in Server.js file,but according to good practising of code,server and routing configuration should be different

Now time to run your application,but still there is some points in Server.js file ,where we have to focus

  • require('./router/main')(app); // we need main.js file in Server.js,require like import,we are passing instance of express into function inside in main.js

  • app.set('views',__dirname + '/views'); // there we set the path of our views folder

  • app.set('view engine', 'ejs'); // we are setting up ejs engine,ejs is used to render html pages. For more details of EJS 

  • app.engine('html', require('ejs').renderFile); // this one is rendering our html pages

Everything is done,Let's run our code(!!!Yippee)

To start your server,run your server.js file by using command “node Server.js”,now you will see on you console printing “Express Server Starting on port 3000”

I hope it will help you to create your first application with Express NodeJS

Thank you

Prabjot Singh

About Author

Author Image
Prabjot Singh

Prabjot is a Java and Grails developer.He has a good hands on neo4J, AngularJS. He likes to work on new technologies

Request for Proposal

Name is required

Comment is required

Sending message..