NODE JS

How to make HTTP module in NODE JS || How to make API in Node file.

  • Neel Bhardwaj

  • 15-Jan-2022

  • 0 comments

  • 120

For create API in NODE JS. Use the HTTP module. It is the predefine module.

In this first getting the data in ARRAY of Object

Then it change into JSON formate by JSON.stringify() function.

 

var http = require('http');

const server = http.createServer((request,response)=>{

   var data = [

   {name:"anonymous"}

]

response.writeHead(200,{'content-type','application/json'});

   response.write(JSON.stringify(data));

   response.end();

});

server.listen(3000,()=> console.log("Server run"));

 

SOME KEY POINT : 

For convert 

var data = [

     {name:"anonymous"}

]

 

TO

 

var data = [

       {"name":"anonymous"}

]

 

Use JSON.stringify(data);

"Programming isn't about what you know,
it's about what you can figure out.”
Chris Pine

Neel Bhardwaj

A full stack developer. I have a Passion for Creating Clean, Beautiful, Interactive, Minimalistic, Responsive and User Friendly website, Developed by Modular, Scalable and Functional Code.

Recent comments

Leave a reply