Untitled
unknown
javascript
3 years ago
4.6 kB
10
Indexable
var http = require('http');
var url = require('url');
var fs = require('fs');
var crypto = require('crypto');
const requestListener = function (req, res) {
var q = url.parse(req.url, true);
const myRoute = "/"+q.pathname.split("/")[1];
switch (myRoute) {
case "/api":
if (req.method === "GET") {
//response headers
res.writeHead(200, { "Content-Type": "application/json" });
//set the response
res.write("Hi there, This is a Vanilla Node.js API");
//end the response
res.end();
}
// If no route present
else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Route not found" }));
}
break;
case "/users":
if (req.method === "GET") {
const readuserdb = fs.readFileSync('userdb.json', {encoding:'utf8'});
console.log(readuserdb);
//response headers
res.writeHead(200, { "Content-Type": "application/json" });
//set the response
res.write(readuserdb);
//end the response
res.end();
}
else if (req.method === "POST") {
const chunks = [];
req.on("data", (chunk) => {
chunks.push(chunk);
});
req.on("end", () => {
//console.log("all parts/chunks have arrived");
const data = Buffer.concat(chunks);
const newuser = JSON.parse(data);
const uuid = crypto.randomBytes(16).toString("hex");
newuser.id=uuid
console.log(newuser)
//const userdbjson = JSON.stringify(userdb);
const readuserdb = fs.readFileSync('userdb.json', {encoding:'utf8'});
const alluser = JSON.parse(readuserdb);
alluser.push(newuser);
//console.log(alluser);
//console.log("Data: ", userdb);
fs.writeFileSync('userdb.json', JSON.stringify(alluser));
});
res.writeHead(200, { "Content-Type": "text/html" });
//set the response
res.write("MSG POST");
//end the response
res.end();
}
else if (req.method === "PUT") {
res.writeHead(200, { "Content-Type": "text/html" });
//set the response
res.write("MSG PUT");
//end the response
res.end();
}
// If no route present
else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Route not found" }));
}
break;
case "/user":
//console.log(q.pathname);
const id = q.pathname.split("/")[2];
console.log(id);
arr = [];
const readuserdb = fs.readFileSync('userdb.json', {encoding:'utf8'});
for(let i = 0; i < readuserdb.length; i++) {
let obj = readuserdb[i];
console.log(obj["id"]);
}
if (req.method === "GET") {
res.writeHead(200, { "Content-Type": "application/json" });
//set the response
res.write("MSG GET");
//end the response
res.end();
}
else if (req.method === "PUT") {
res.writeHead(200, { "Content-Type": "application/json" });
//set the response
res.write("MSG PUT");
//end the response
res.end();
}
else if (req.method === "DELETE") {
res.writeHead(200, { "Content-Type": "application/json" });
//set the response
res.write("MSG DEL");
//end the response
res.end();
}
break;
case "/":
res.writeHead(200, { "Content-Type": "text/html" });
//set the response
res.write("<h1>Welcome</h1>");
//end the response
res.end();
break;
default :
res.writeHead(200, { "Content-Type": "text/html" });
//set the response
res.write("<h1>404 Not Found</h1>");
//end the response
res.end();
break;
}
};
const host = 'localhost';
const port = 8000;
const server = http.createServer(requestListener);
server.listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
});
Editor is loading...