// FileName: student.json
[
{
"name": "Pankaj Agarwal",
"office" : "Magnet Brains Software Technology Pvt. Ltd",
"project" : "pabbly.com"
},
{
"name": "Ramesh Chandra",
"office" : "Magnet Brains",
"project" : "woofresh.com"
},
{
"name": "Suk Sagar",
"office" : "Magnet",
"project" : "formget.com"
},
{
"name": "Rakesh Roy",
"office" : "Magnet Brains Software Technology Pvt. Ltd",
"project" : "pabbly.com"
}
]
sdfsdf
// Reading JSON Files Using Node.js
// Generating Right JSON from Routing Using Node.js
// JSON Files: student.json
// Get Complete Source Code from Pabbly.com
var http = require('http');
var staffdata = require('./public/data/student.json');
var server = http.createServer(function(req, res){
if(req.url === "/"){
res.writeHead(200, {"Content-Type": "text/json"});
res.end(JSON.stringify(staffdata));
}else if(req.url === "/pabbly.com"){
var data = staffdata.filter(function(item){
return item.project === "pabbly.com";
});
res.writeHead(200, {"Content-Type": "text/json"});
res.end(JSON.stringify(data));
}else if(req.url === "/formget.com"){
var data = staffdata.filter(function(item){
return item.project === "formget.com";
});
res.writeHead(200, {"Content-Type": "text/json"});
res.end(JSON.stringify(data));
}
else{
res.writeHead(404, {"Content-Type": "text/html"});
res.end("Not Page Found");
}
});
server.listen(3000, function(){
console.log("We are listing at port number 3000");
});
Learn More-