In: Computer Science
1. Create a NodeJS application having name
student (1 Point)
2. Use expressJS framework to create REST API to perform
given
task. (1 Point)
3. Read the given users.json file and return the requested
details
by client.
A. Endpoint /user?uid=? (4 Points)
Return JSON response having following fields
{
"id": 1,
"name": "john smith",
"email": "[email protected]",
"address": "street name, city, zipcode",
"phone": "1-770-736-8031"
}
If no user id found, then return
{
"message": "No user found"
}
B. Endpoint /users/all (4 Points)
Return all user details in ascending order (Use username
field as sorting key)
app.js
const express = require("express");
const cors = require("cors");
const logger = require("morgan");
const bodyParser = require("body-parser");
const compression = require("compression");
const fs = require("fs");
const app = express();
app.use(compression());
app.use(cors());
app.use(logger("dev"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const readFile = path => {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, data) => {
if (err) reject(err);
else resolve(JSON.parse(data.toString()));
});
});
};
app.get("/user", (req, res) => {
const { uid } = req.query;
readFile("users.json")
.then(users => {
users = users.filter(user => user.id == uid);
if (users.length == 1) return res.status(200).send(JSON.stringify(users[0]));
else return res.status(400).send(JSON.stringify({ message: "no user found" }));
})
.catch(err => {
console.log(err);
res.status(500).send(JSON.stringify({ message: "Something went wrong" }));
});
});
app.get("/users/all", (req, res) => {
readFile("users.json")
.then(users => {
users.sort((a, b) => {
return a.name.localeCompare(b.name);
});
if (users.length >= 1) return res.status(200).send(JSON.stringify(users));
else return res.status(400).send(JSON.stringify({ message: "no user found" }));
})
.catch(err => {
console.log(err);
res.status(500).send(JSON.stringify({ message: "Something went wrong" }));
});
});
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Server started running on port ${PORT}`);
});
users.json
[
{
"id":128393,
"name":"Sooraj Shukla",
"email":"[email protected]",
"address":"street name, city, zipCode",
"phone":"+919238283232"
},{
"id":939282,
"name":"Ashish Jaiswal",
"email":"ashish12312@somewherecom",
"address":"street name, city, zipCode",
"phone":"+918238283232"
},{
"id":239292,
"name":"Ram Singh",
"email":"[email protected]",
"address":"street name, city, zipCode",
"phone":"+919238298732"
},{
"id":238212,
"name":"Nancy Mishra",
"email":"[email protected]",
"address":"street name, city, zipCode",
"phone":"+919938283232"
},{
"id":392392,
"name":"Priyank Gupta",
"email":"[email protected]",
"address":"street name, city, zipCode",
"phone":"+918328837232"
},{
"id":129831,
"name":"Ayushi Mishra",
"email":"[email protected]",
"address":"street name, city, zipCode",
"phone":"+919238283232"
}
]
package.json
{
"name": "student-app",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Sooraj Shukla",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"compression": "^1.7.4",
"cors": "^2.8.5",
"express": "^4.17.1",
"morgan": "^1.10.0"
}
}