a blog for those who code

Saturday, 28 May 2016

How to handle HTTP requests in Node.js Application

In this post we will be discussing about handling different HTTP requests in Node.js Application. There might be scenario in your application where you want to differentiate the incoming requests based on their type. So, we will be implementing handler functions for each type of the requests (Get, Post, Put, Head, Bad and Delete).

Instead of writing the handler inside our server file, we will create a module (http-handler) where we have entire request handling functionality will be wrapped. Let's start by creating a file http-handler.js which will server as a module for us.

function get_request(request, response) {
  response.writeHead(200, {
    'Content-Type' : 'text/plain'
  });
  response.end('Handled Get Request');
}

function post_request(request, response) {
  response.writeHead(200, {
    'Content-Type' : 'text/plain'
  });
  response.end('Handled Post Request');
}

function delete_request(request, response) {
  response.writeHead(200, {
    'Content-Type' : 'text/plain'
  });
  response.end('Handled Delete Request');
}

function put_request(request, response) {
  response.writeHead(200, {
    'Content-Type' : 'text/plain'
  });
  response.end('Handled Put Request');
}

function head_request(request, response) {
  response.writeHead(200, {
    'Content-Type' : 'text/plain'
  });
  response.end('Handled Head Request');
}

function bad_request(request, response) {
  response.writeHead(400, {
    'Content-Type' : 'text/plain'
  });
  response.end('Handled Bad Request');
}

exports.http_handler = function(request, response) {
  switch (request.method) {
    case 'GET' :
      get_request(request, response);
      break;
    case 'POST' :
      post_request(request, response);
      break;
    case 'DELETE' :
      delete_request(request, response);
      break;
    case 'PUT' :
      put_request(request, response);
      break;
    case 'HEAD' :
      head_request(request, response);
      break;
    default :
      bad_request(request, response);
      break;
    }
    console.log('Request Processing Done');
}

The above file will create a user-defined module that exports the http_handler function to the outer components.

Now we can access our module from httpReq.js file by adding a require of the http_handler.js file and calling the function http_handler as shown below :

var httpHandler = require('./http-handler');
var http = require('http');
var port = 3000;

http.createServer(httpHandler.http_handler).listen(port, '127.0.0.1');

When you run the localhost:3000 in the postman with Get or Post request you will see that how we are handling the different methods as shown below :





Note : The above code is just for make you understand that how the HTTP handler works. Luckily we do not have to do this because Express Framework will do this for us. This is just a clear example of the Node.js capabilities of handling HTTP requests.

Please Like and Share CodingDefined.com Blog, if you find it interesting and helpful.

2 comments:

  1. Or just npm install request

    ReplyDelete
    Replies
    1. You could, but sometimes it's good to know how to do it without a module.

      Delete

Contact Us

Name

Email *

Message *

Sponsers

Best Hosting Plans

CC License