अभी तक आपने सीखा कि NodeJS में modules क्या होते हैं , और कैसे create करते हैं। और जो भी test examples आपने देखे वो command line पर ही देखे थे। इस topic में आप सीखेंगे कि NodeJS में http requests को कैसे handle करते हैं।

NodeJS http Module

NodeJS में http requests को core module http की help से handle किया जाता है। जिसमे http requests को handle करने के लिए कई functions defined होते हैं। इन्ही functions का use करके हम http requests को handle करने कि कोशिश करेंगे।

NodeJS createServer

http request handle करने के लिए createServer(request , response) method का use किया जाता है , जिसमे 2 parameters (request, response) include होते हैं जो कि NodeJS द्वारा supply किये जाते हैं। request का use http request information like : url, request header etc. get करने के लिए किया जाता है , जबकि response का use current request के लिए response send करने के लिए किया जाता है।


बाकी अगर आप इन दोनों variables लो console.log() में print कराकर देखोगे , तो आपको complete information पता चल जायगी।

File : app.js

Copy Fullscreen Close Fullscreen Run
// import http module.
const http = require('http'); 
// define port.
const port = 8080;
// create server.
const server = http.createServer(function(req, res){
     //here we can access all the incoming http requests.

     // define headers and response message.
    res.writeHead(200, {'Content-Type':'text/html'});
    res.write("Awesome ! it's working");
    res.end();
}).listen(port);

console.log("Node.js web server at localhost:"+port+" is running..");
C:\Users\HP\Desktop\workspace\nodejs>node app.js
Node.js web server at localhost:8080 is running..

That's it, अब browser open करें और search bar में type करें : http://localhost:8080/

Note : ध्यान रहे code में कोई भी changes करने पर आपको server फिर से start करना पड़ेगा , मतलब जितनी बार code change होगा वो file भी उतनी बार run करनी पड़ेगी।

nodeJS handle requests with different path

अभी ऊपर सिर्फ एक ही url के साथ example दिया है , different - different path की requests handle करने के लिए request.url property से url को match करके handle कर सकते हैं।
See Example :

File : app.js

Run Fullscreen Close Fullscreen Copy
const http = require('http'); 
const port = 8080;
const server = http.createServer(function(req, res){
    // using req.url proprties, we can handle request for diffrent types of url.
   const url = req.url;
   if(url == '/')
   {
       res.writeHead(200, {'Content-Type':'text/html'});
       res.write("Welcome Home");
       res.end();
   }
   else if(url== '/user')
   {
       res.writeHead(200, {'Content-Type':'text/html'});
       res.write("User Dashboard");
       res.end();
   }
   else if(url== '/admin')
   {
       res.writeHead(200, {'Content-Type':'text/html'});
       res.write("Admin Dashboard");
       res.end();
   }
   else
       res.end('Invalid Request!');

}).listen(port);

console.log("Node.js web server at localhost:"+port+" is running..");
C:\Users\HP\Desktop\workspace\nodejs>node app.js
Node.js web server at localhost:8080 is running..

file को run करने के बाद अब आप , http://localhost:8080/user या http://localhost:8080/admin url भी access कर सकते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers