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

1-Create a new Visual C# Console App (.NET Framework). Name the solution "GPA Calculation". 2-Zip and...
1-Create a new Visual C# Console App (.NET Framework). Name the solution "GPA Calculation". 2-Zip and Submit your entire project with the solution file GPA Ask the user to input their grade percentage (e.g. the use will enter 98 if they had an overall grade of 98% in their course) for their Accounting, Marketing, Economics and MIS courses. Assume each course is worth 3 credit hours. Once you have all of their grades output what letter grade they earned for...
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...
Question 1 (10) Create a class Student with public member variables: Student name, student number, contact...
Question 1 (10) Create a class Student with public member variables: Student name, student number, contact number, ID number. The following specifications are required:  Add init() method of the class that initializes string member variables to empty strings and numeric values to 0. (2)  Add the method populate() to the class. The method is used to assign values to the member variables of the class. (2)  Add the method display() to the class. The method is used...
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...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All the attributes must be String) Create a constructor that accepts first name and last name to create a student object. Create appropriate getters and setters Create another class StudentOperationClient, that contains a main program. This is the place where the student objects are created and other activities are performed. In the main program, create student objects, with the following first and last names. Chris...
Create a C# console application (do not create a .NET CORE project) and name the project....
Create a C# console application (do not create a .NET CORE project) and name the project. Generate two random integers, each between 1 and 50, that you will be adding together to test the user's ability to perform the addition operator. Display the numbers in the console, such as:             7 + 22 = ? Once the user provides their answer, check to see if it is correct and if not, tell them sorry, please try again. If their answer...
Create a C# console application (do not create a .NET CORE project) and name the project...
Create a C# console application (do not create a .NET CORE project) and name the project TuitionIncrease. The college charges a full-time student $12,000 in tuition per semester. It has been announced that there will be a tuition increase by 5% each year for the next 7 years. Your application should display the projected semester tuition amount for the next 7 years in the console window in the following format:             The tuition after year 1 will be $12,600. Note:...
Create a C# console application (do not create a .NET CORE project) and name the project...
Create a C# console application (do not create a .NET CORE project) and name the project TimeToBurn. Running on a particular treadmill, you burn 3.9 calories per minute. Ask the user how many calories they wish to burn in this workout session (this is their goal). Once they tell you, output on the console after each minute, how many calories they have burned (e.g. After 1 minute, you have burned 3.9 calories). Keep outputting the total amount of calories they...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT