Node.js – Express Framework

In this tutorial, we are going to be talking about Express Framework in Node.js.

You will learn what the Express framework is. You’ll also learn how to set up and use Express Framework.

  1. Overview of Express Framework
  2. Setting up Express Framework
  3. A Simple Example
  4. Request and Response
  5. Basic Routing

 

1. Overview of Express Framework

The Express Framework also called express.js is a web application server framework that works with Node.js. You can used it to quickly build web applications like single-page applications and mobile applications.

Features of the express framework include:

  • can server as template engine for rendering html pages
  • provides routing table for managing http requests
  • enables setting up of middleware for responding to http request

 

 2. Setting up Express Framework

To set up express simply run the command

npm install express

After running this command, a folder is created in your application folder containing all the downloaded modules.

You’ll also need to install the following additional modules:

  • body-parser – for handling different format type including json, row, text and url-encoded form data
  • cookie-parser – used or parsing cookie header  and getting cookie values
  • multer – for handling form data

 

3. A simple Example

Let’s write a simple express app that creates and starts a web server listening at port 8080. When a request comes from a client, the app responds with a message ‘Welcome to Express Framework’.

//Demo Express App by Kindson The Genius
var express = require('express');
var app = express();

app.get('/', function(req, res){
    res.send('Welcome to Express');
})

var server = app.listen(8080, function(){
    var host = server.address().address
    var port = server.address().port

    console.log("Demo web app listening at http://%s:%s", host, port)
})

 

Run the code above. The visit http://localhost:8080, then you can see the page displays with the message ‘Welcome to Express’.

 

4. Request and Response in Express

An application create with express makes use of callback function. The function would take two parameters: request and response. This is as shown below

 

app.get('/', function(req, res){
    res.send('Welcome to Express');
})

 

  • A request object represents a http request coming from a client. It has all the properties of a request including url query string, request body, http header  and body
  • A response object represents  a http response provided by the server

You can also obtain the properties of the request and response objects to examine them or print them out to the console

 

5. Routing Using Express

Now we would talk about something very important: routing.

How do control the endpoint that is reached based on the url path provided?  Also routing helps us route a request based on the http method. For instance a POST request, GET request, PUT or DELETE request.

Let’s work with our previous demo webapp. This time, we’ll extend it to include routing feature. This is as easy as just changing app.get() to app.post() and a little more. Let’s do it

//Demo Express App by Kindson The Genius
var express = require('express');
var app = express();

// Handles a "GET Request for home" on the homepage
app.get('/', function (req, res) {
   console.log("GET request for the homepage");
   res.send('Welcome to GET!');
})

// Handles a POST request for the homepage
app.post('/', function (req, res) {
   console.log("POST request for the homepage");
   res.send('Welcome to POST!');
})

// Handles a DELETE request for the /del_user page.
app.delete('/del_user', function (req, res) {
   console.log("DELETE request coming from /del_user");
   res.send('Welcome to DELETE!');
})

// Handles a GET request for the /list_user page.
app.get('/list_user', function (req, res) {
   console.log("GET Request comming from /list_user");
   res.send('List of User');
})

// Handles a get request whose part correspond to the url pattern
//wxyz, wxayz, wxmyyz etc
app.get('/wx*yz', function(req, res) {   
   console.log("GET Request comming from /wx*yz");
   res.send('URL Pattern Match');
})

var server = app.listen(8080, function () {
   var host = server.address().address
   var port = server.address().port
   
   console.log("Demo app listening at http://%s:%s", host, port)
})

Save the code and then run it.

Try to visit these urls and observe the output

  • http://localhost/8080
  • http://localhost/list_user

In the next part, I will teach you how to use Advanced REST Client to make DELETE and POST requests