Node.JS Express Framework

In the article on NPM, we explained how NPM can be used to import online modules. As an example we download the express module. In this article we are going to study that express module in detail. Node.js Express framework is one of the most widely and frequently used frameworks. It contains functionalities that foster rapid development of web and mobile applications. The two major functionalities that node.js express framework provide are the ability to provide middle wares that can interact between HTTP requests and responses and the ability to render dynamic pages based upon user requests. Enough of theory, let’s create a simple node.js express framework application that returns welcome message to the user.

Node.js Express Framework Hello World Example

If you dont have express framework installed, type the following command in your node.s command window “npm install express –save”. Now let’s create a simple application that uses node.js express framework.

var express = require('express');
var expapp = express();

expapp.get('/', function (req, res) {
   res.send('Welcome to knowledge hills');
})

var rserver = expapp.listen(9006, function () {
   var hostaddr = rserver.address().address
   var portaddr = rserver.address().port
   
   console.log("Server listening at http://%s:%s", hostaddr, portaddr)
})

Download the code Try the code

Take a look at the above code. Here we again use the “get” method of the express class. This method takes the the path as the first parameter. The second parameter is the callback function that executes whenever a user requests for that path. In this case the path is the root folder which contains the code. For instance run the above code and then type the following URL in the browser “http://127.0.0.1:9006/”. This will display the message “Welcome to knowledge hills”

Basic Routing in Express Framework

In the last example we had a get method that displayed a simple string whenever a user requested for home page.The real beauty of express framework is the use of different HTTP methods and multiple paths. Take a look at the following example.

var express = require('express');
var expapp = express();



expapp.get('/list_student', function (req, res) {

   res.send('Page Contains List of All Students');
})

expapp.get('/ab*cd', function(req, res) {   
   res.send('Request arrived for abXcd. Where x can be anything');
})

var rserver = expapp.listen(9006, function () {

   var hostaddr = rserver.address().address
   var portaddr = rserver.address().port

   console.log("server running at http://%s:%s", hostaddr, portaddr)
})

Download the code Try the code

In the above code, there are two get methods. The first get method has a path “/list_student”. This method shall execute when user enters “http://127.0.0.1:9006/list_student” in the browser. Similarly, the second get method has a path “/ab*cd”. This method shall execute when user enters path that starts with “ab” and ends with “cd”. There can be anything between them

Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .