How to make HTTP module in NODE JS || How to make API in Node file.
-
Neel Bhardwaj
-
15-Jan-2022
-
0 comments
-
165
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.”
Leave a reply
Thanks for comment. Your comment are shown after approve by ADMIN. THANKS!!
There is some server issue. Please try again. THANKS!!
Fatal error: Cannot declare class Connection, because the name is already in use in /home/d8ouyzxnuef4/public_html/thecoderjob.com/cj_admin/classes/Connection.php on line 3
Recent comments