Question

In: Computer Science

1. Create a NodeJS application having name student (1 Point) 2. Use expressJS framework to create...

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)

Solutions

Expert Solution

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"
  }
}

Related Solutions

Create a python application that inputs, processes and stores student data. Specifications: 1. Your application should...
Create a python application that inputs, processes and stores student data. Specifications: 1. Your application should be able to accept Student data from the user and add the information in a file type of your choice. 2. your application is a menu driven and allow the user to choose from the following menu Menu: 1 – Add students to file 2 – print all the student information 3 – print specific student information using studentID 4 – Exit the program...
Create a class called Student which stores • the name of the student • the grade...
Create a class called Student which stores • the name of the student • the grade of the student • Write a main method that asks the user for the name of the input file and the name of the output file. Main should open the input file for reading . It should read in the first and last name of each student into the Student’s name field. It should read the grade into the grade field. • Calculate the...
Student Structure Assignment Create a Student Structure globally consisting of student ID, name, completed credits, and...
Student Structure Assignment Create a Student Structure globally consisting of student ID, name, completed credits, and GPA. Define one student in main() and initialize the student with data. Display all of the student’s data on one line separated by tabs. Create another student in main(). Assign values to your second student (do not get input from the user). Display the second student’s data on one line separated by tabs. Create a third student in main(). Use a series of prompts...
For this assignment, You will create a NodeJS application which takes a city name as an...
For this assignment, You will create a NodeJS application which takes a city name as an input in its pug template. using openweathermap API, you should get the weather of that particular city and displays that information to a new pug template. You should also store the results in a file in your directory. Following are the detailed requirements. Your application should start with a pug template (similar to HTML page) which has a form with an input field and...
Create a Java application that will prompt the user for the first name of 6 friends...
Create a Java application that will prompt the user for the first name of 6 friends in any order and store them in an array. First, output the array unsorted. Next, sort the array of friends and then output the sorted array to the screen. Be sure to clearly label the output. See the example program input and output shown below. It does not have to be exactly as shown in the example, however it gives you an idea of...
Create an application for a library and name it FineForOverdueBooks. TheMain() method asks the user to...
Create an application for a library and name it FineForOverdueBooks. TheMain() method asks the user to input the number of books checked out and the number of days they are overdue. Pass those values to a method named DisplayFine that displays the library fine, which is 10 cents per book per day for the first seven days a book is overdue, then 20 cents per book per day for each additional day. The library fine should be displayed in the...
In this question, use your new GUI classes to create a small application. You have 2...
In this question, use your new GUI classes to create a small application. You have 2 options: Write any small application of your choice, as long as it follows the rules that follow. Write the small UnitConversion application, which will be described for you. Option 1: Write your own application You can write any small application that you like, as long as: It uses your GUI classes, including: at least one checkbox, at least one group of 3 radio buttons,...
In C# Create a windows application which accepts the month number and displays the month name...
In C# Create a windows application which accepts the month number and displays the month name in a label.   Use a nested if... else statement to determine the month name. For months not in the range 1-12 display the message "Not a valid month"
1. Create a class Car with data: Name, Price, Production and properly methods. 2. Create another...
1. Create a class Car with data: Name, Price, Production and properly methods. 2. Create another class named GenericCar with a parameter of the T type. This class manages a collection of object T (may be LinkedList) named a. Implementing some methods for GenericCar: Add: add new item of T to a Display: display all items of a getSize: return the number item of a checkEmpty: check and return whether a is empty or not delete(int pos): remove the item...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class Create another class called CourseSection Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of the methods required for a standard user defined...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT