Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs Application #9

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added FinalReport.pdf
Binary file not shown.
9 changes: 9 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require("fs"); // require the fs module to manipulate with file system
const express = require("express"); // requiring the express module
const app = express();
const morgan = require("morgan")
app.use(morgan("dev")); // using a morgan middleware to get the requested url at the client side to print at the console
app.use(express.json()); // using express.json() middleware to further read the body the code to handle post ,patch,put requests
const userRouters = require("./userRoutes"); // Importing the packages rounters that routes the data acoording to the http methods
app.use("/", userRouters); // whenver the home page is requested the control flow goes to the userRoute.js which routes according to the url
module.exports = app; // exporting to be used by the server.js file which is the starting file
8 changes: 8 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*This is starting file of the nodejs application */
const app = require("./app"); //
const port = 3000;

/*Starting server at port 3000 using the listen function*/
app.listen(port, () => {
console.log("The server is started and listens at port 3000");
});
109 changes: 109 additions & 0 deletions userControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const fs = require("fs"); // requiring the module to manipulate with file system
const userJSON = JSON.parse(fs.readFileSync(`${__dirname}/data/users.json`, { encoding: "utf-8" })); // reading the json which is sample data created by me which consist of user info
const findObject = (getId) => {
for (let i = 0; i < userJSON.length ; i++) {
if (userJSON[i] != null && userJSON[i].id === getId) {
return userJSON[i];
}
}
return undefined
}

/*The url is get all the users names thus we applied a get request method send the entire json file with respective status*/
exports.getAllUsers = (requestUsers, responseUsers) => {
responseUsers.status(200).json({
status: "OK",
data: {
userJSON
}
});
}
/*This url the client sends the data as json object and we write into the json file a post request*/
exports.postUsers = (requestUsers, responUsers) => {
const newObject = requestUsers.body;
userJSON.push(newObject);
fs.writeFile(`${__dirname}/data/users.json`, JSON.stringify(userJSON), (err) => {
responUsers.status(201).json({
status: "Written successfully",
data: {
newObject
}
});
})
}

/*This url an id is mentioned additionally which should inturn fetch the particular user with the respective id */
exports.getSpecificUser = (requestUser, responseUser) => {
const getId = Number(requestUser.params.id);
const getData = findObject(getId);
console.log(getData)
if (!getData) {
responseUser.status(404).json({
status: "Invalid Id",
message: "Data not found"
})
}
else {
responseUser.status(200).json({
status: "OK",
data: {
getData
}
});
}

}

/*A patch request is update a key for particular json object in the json file with respective to user ID*/
exports.updateUser = (requestUsers, responseUsers) => {
const getId = Number(requestUsers.params.id);
const getObject = findObject(getId);
if (!getObject) {
responseUsers.status(404).json({
status: "Not found",
message : "Invalid data"
});
}
else {
const getId = Number(requestUsers.params.id);
const getCollege = requestUsers.body.college
console.log(getCollege);
userJSON[getId].college = getCollege;
fs.writeFile(`${__dirname}/data/users.json`, JSON.stringify(userJSON), (err) => {
responseUsers.status(201).json({
status: "Updated successfully",
data: {
getObject
}
});
})
}
}

/*A delete request is used to delete a json object with according to the user Id specified in the url params*/
exports.deleteUser = (requestUser, responseUser) => {
const getId = Number(requestUser.params.id);
const getObject = findObject(getId);
console.log(getObject)
if (!getObject) {
responseUser.status(404).json({
status: "Not found",
message : "Invalid data"
});
}
else {
const getObject = userJSON.find(data => {
if(data != null)
data.id === getId
})
delete userJSON[getId];
fs.writeFile(`${__dirname}/data/users.json`, JSON.stringify(userJSON), (err) => {
responseUser.status(204).json({
status: "Deleted successfully",
data: {
getObject
}
});
})
}
}
14 changes: 14 additions & 0 deletions userRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const express = require("express");
const userApp = express.Router(); // mounting a rounter to handle the http request accordingly
const userController = require("./userControllers"); //The application logic to handle the http request stored in userController.js file following the MVC model

userApp
.route("/api/users")
.get(userController.getAllUsers)
.post(userController.postUsers);
userApp
.route("/api/users/:id")
.get(userController.getSpecificUser)
.patch(userController.updateUser)
.delete(userController.deleteUser);
module.exports = userApp;
1 change: 1 addition & 0 deletions users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[null,{"id":1,"firstName":"ghi","lastName":"jkl","cgpa":9.5,"college":"Guidy College of Engineering","SSLC":95,"HSC":94},{"id":2,"firstName":"mno","lastName":"pqr","cgpa":8.8,"college":"IIIT ,Delhi","SSLC":89,"HSC":98},{"id":3,"firstName":"stu","lastName":"vw","cgpa":8.2,"college":"Indian Institute of Technology","SSLC":100,"HSC":96},{"id":4,"firstName":"yz","lastName":"abc","cgpa":9.2,"college":"National Institute of Technology","SSLC":90,"HSC":92},{"id":0,"firstName":"abcdef","lastName":"1234","cgpa":9,"college":"Indian Institute of Information Technology","SSLC":99,"HSC":99},{"id":0,"firstName":"abcdef","lastName":"1234","cgpa":9,"college":"Indian Institute of Information Technology","SSLC":99,"HSC":99}]