Express Js Introduction

Express js Introduction

express() – Creates an Express application. The express() function is a top-level function exported by the express module.

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

express has 4 major objects,

  1. Application

  2. Request

  3. Response

  4. Router

1) Application :

The app object conventionally denotes the Express application. Create it by calling the top-level express() function exported by the Express module:

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

app.get('/', function(req, res){
  res.send('hello world');
});

app.listen(3000);

The app object has methods for

  • Routing HTTP requests; see for example, app.METHOD and app.param.

  • Configuring middleware; see app.route.

  • Rendering HTML views; see app.render.

  • Registering a template engine; see app.engine.

2) Request :

The req object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.

Usually the object is always referred to as req (and the HTTP response is res) but its actual name is determined by the parameters to the callback function in which you’re working.

app.get('/user/:id', function(req, res) {
  res.send('user ' + req.params.id);
});

3) Response

The res object represents the HTTP response that an Express app sends when it gets an HTTP request.

app.get('/user/:id', function(req, res){
  res.send('user ' + req.params.id);
});

4) Router

A router object is an isolated instance of middleware and routes. We can say it as a “mini-application,” capable only of performing middleware and routing functions. Every Express application has a built-in app router.

A router behaves like middleware itself, so you can use it as an argument to app.use() or as the argument to another router’s use() method.

The top-level express object has a Router() method that creates a new router object.

Once you’ve created a router object, you can add middleware and HTTP method routes ( get, put and so on) to it just like an application. For example:

// invoked for any requests passed to this router
router.use(function(req, res, next) {
  // .. some logic here .. like any other middleware
  next();
});

// will handle any request that ends in /events
// depends on where the router is "use()'d"
router.get('/events', function(req, res, next) {
  // ..
});

You can then use a router for a particular root URL in this way separating your routes into files or even mini-apps.


// only requests to /calendar/* will be sent to our "router"
app.use('/calendar', router);

The Complete Example for router Example : sample.js


var express = require('express');
var app = express()
var router = express.Router()

// a middleware function with no mount path. This code is executed for every request to the router
router.use(function (req, res, next) {
  // .. some logic here .. like any other middleware
  var currentDate = new Date;
  console.log('Time:', currentDate.toLocaleString() )
  next()
})

// a middleware sub-stack shows request info for any type of HTTP request to the /user/:id path
router.use('/user/:id', function (req, res, next) {
  console.log('Request URL:', req.originalUrl)
  next()
}, function (req, res, next) {
  console.log('Request Type:', req.method)
  next()
})

// a middleware sub-stack that handles GET requests to the /user/:id path
router.get('/user/:id', function (req, res, next) {
  // if the user ID is 0, skip to the next router
  if (req.params.id === '0') next('route')
  // otherwise pass control to the next middleware function in this stack
  else next()
}, function (req, res, next) {
    console.log('regular');
  // render a regular page
  res.send('regular')
})

// handler for the /user/:id path, which renders a special page
router.get('/user/:id', function (req, res, next) {
  console.log(req.params.id)
  res.send('special')
})

// mount the router on the app
app.use('/', router)

app.listen(3000, function() {
    console.log('Listenint Port : 3000 ');
});

Kill Nodejs Process stack

taskkill /F /IM node.exe

Leave a Reply

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