Home Scripting Node.js Node.js Express: Definition, Installation, Project structure, Example

Node.js Express: Definition, Installation, Project structure, Example

1 858

node.js

Now that you know how to install node.js and how to write simple programs, you will go further and learn

  • What is express?
  • How to install express
  • Project structure of express framework
  • Execute sample programs

Introduction

What is express?

Express is a web application framework for node.js. It provides all the low level services for web app development so that the developer can focus on the business logic of the application.
Express is installed as a module in node.js.

How to install a module?

You can install any module using node package manager(npm). Npm is installed by default along with node. To check the version of npm goto command prompt(terminal) and type in npm –version.
You can install modules globally: these are available through out the system, local installation: these modules are accessible only to a particular project.
procedure to install module.
->goto->command prompt->type in->npm install -g (here g stands for global environment).
this installs the module in your nodejs root folders->node_module directory. by installing in this location we can access the module throughout the system and not just for a particular project.
If you wish to install any module only for a particular project then first enter that directory and then install the module without -g extension.
ex: d:\>expresstutorial>npm install .

Install express module globally

  • Open terminal(cmd prompt)->typein->npm install express -g
  • Now in order to use this module create a project folder ex: d:\>nodeexpress>typein->express
    This will create a project structure and create necesaary files for you to work with express framework.
    Its time to download the dependencies.
  • Enter the project folder and typein->npm install.
  • This command will install the dependencies which are required.
    By default express framework uses jade templating system.

Node.js express project structure

package.json-> this file has the details of the project: we can mention name of the project/module, version number, dependencies(name-version numbers of other modules the current project is depending on) etc..
app.js->this is where you will spend a lot of time. Here the server object is created, default directories are set..
public folder->holds all the files related to stylesheets, javascript, and images.
routes folder->we can make use of this folder for URL routing.
views folder->views which are generated for presentation will be saved here.

Try this: express example

We will create a web site which serves static web pages.

->First create about.jade, services.jade, products.jade files with content in it and save them in the views folder.
->Open app.js file and replace the existing code with the below code.

var express = require('express')
, http = require('http')
, path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
app.get('/', function(req, res){
res.render('index');
});
app.get('/about', function(req, res){
res.render('about');
});
app.get('/services', function(req, res){
res.render('services');
});
app.get('/products', function(req, res){
res.render('products');
});

Explanation of the example

line1->If we want to use any of the modules, first include them. here we have included express, http, path modules.
line4->var app=express(); the express object is created and it is assigned to the variable and we can access their methods using this variable.
line6->express object setter method is used here to set a port number.
line7->default views directory is set here.
line8->the templating engine is set here.
line16->app.get() function is used to get the url paths and the function(req, res) is the callback function and it renders the described page to the browser.