Quick Contact


    HTTP MODULE

    The powerful building blocks of node is the HTTP module that we use for creating networking applications for examples- create a web server that listens for HTTP requests on a given port and with this you can easily create a back-end service for client applications like web application that you build with react or angular or a mobile application running on mobile device. So, in http there are different classes like http like agent, http like client request. On each class has a bunch of properties, methods and events.

    So now to deal with HTTP module let’s start with code in file usually take i.e. app.JS.

    Let’s load HTTP module as-:

    Here first constant HTTP set to require HTTP. Further call “HTTP.createServer”, this is one of the functions defined in the module and with this create web server. Server has all the capabilities of EventEmitter.

    HTTP server class is inherited from net.Server class and net.Server is an EventEmitter. So it means bunch of nodes core functionality is based on emitter. Now call the server and define the listener. So code will be as-:

    	Const http = require (‘http’);
    	Const server = http.createServer ();
    	Server.listen (3000);
    
    	Console.log (‘Listening on port 3000...’);
    	

    As you already know that at each new connection and new request this server raises an event. So, you can use own method to handle that event. Before listening run a register a listener or a handler is as-:

    Server. On (‘connection’, (socket) => {

    Console.log (‘New Connection…’);

    })

    Now go to the terminal and run the code

    Output is-: You will see “listening on port 3,000”

    http-module

    Now go to browser and check localhost 3000 and once you run you will get “new connection” in terminal. So in code server object raises different kinds of events that you can respond to now in real world application, you are not going to respond to the connection event to build an HTTP service. Pass a call-back function to this create server method, so complete code will be as-:

    Instead of socket you can work with the actual request or response objects so you can check if request that URL equals slash then we can send something to the client for and end with response.

    Example-: “res.write (‘hello world’)” and res.end();

    Then code is as-:

    	Const http = require (‘http’);
    	Const server = http.createServer((req, res) => {
    	           If (req.url === ‘/’) {
    	res.write (‘hello world’);
    	res.end();
    	   }
    	});
    	Server.listen (3000);
    	  Console.log (‘Listening on port 3000...’);
    	Go to terminal and execute the code by writing “app.js”
    	
    Output is-:
    http-module
    Now open browser and run localhost-:
    Output is-:
    http-module

    If you want to build a back-end service for our web or mobile applications you need to handle various routes here for example-:

    	        If (req.url === ‘/api/courses’) {
    	Res.write (JSON.stringify) ([1, 2, 3]));
    	Res.end ();
    	         }
    	         });
    	

    Copyright 1999- Ducat Creative, All rights reserved.