Image description

PROGRAD LABS | NODE | REST API - PROUSERS

Learning Goals

In this exercise, you will learn how to create REST api end points:

  • Building a RESTful API.
  • Performing CRUD operations.
  • Writing API endpoints.

Getting started

  1. Fork this repo
  2. Clone this repo

Whenever you create a first significant change, you should make your first commit.

  1. Follow these guidelines to add, commit and push changes.

In the end of this document, you will find guidelines on how to submit the exercise.

Introduction

Use Node.js and Express to build an API that performs CRUD operations on users.

  • Add a .gitignore file appropriate for node.js projects.
  • Add a new package.json.
  • Add a server script to package.json that runs the API using nodemon.

PROGRESSION 1 | ESTABLISH CONNECTION

Your task in this iteration is to create a

  • EXPRESS server.
  • An account in Mongo Atlas
  • connection between node and atlas using mongoose.

PROGRESSION 2 | MODEL IT

Create a prograd user model class schema

{
  // hint: use the shortid npm package to generate it
  name: "Pro Grad", // String, required
  email: "faceprep@prograd.in",  // String, required
  age: 3,
  prograd_id: 001,
  squad: 64
}

PROGRESSION 3 | CREATE ENDPOINTS

Add the code necessary to create a Web API and implement the following endpoints:

Method URL Description
POST /api/users Creates a user using the information sent inside the request body.
GET /api/users Returns an array users.
GET /api/users/:id Returns the user object with the specified id.
DELETE /api/users/:id Removes the user with the specified id and returns the deleted user.
PUT /api/users/:id Updates the user with the specified id using data from the request body. Returns the modified user

Endpoint Specifications

When the client makes a `POST` request to `/api/users`:

- If the request body is missing the `name` or `bio` property:

  - respond with HTTP status code `400` (Bad Request).
  - return the following JSON response: `{ errorMessage: "Please provide name and bio for the user." }`.

- If the information about the _user_ is valid:

  - save the new _user_ the the database.
  - respond with HTTP status code `201` (Created).
  - return the newly created _user document_.

- If there's an error while saving the _user_:
  - respond with HTTP status code `500` (Server Error).
  - return the following JSON object: `{ errorMessage: "There was an error while saving the user to the database" }`.

When the client makes a `GET` request to `/api/users`:

- If there's an error in retrieving the _users_ from the database:
  - respond with HTTP status code `500`.
  - return the following JSON object: `{ errorMessage: "The users information could not be retrieved." }`.

When the client makes a `GET` request to `/api/users/:id`:

- If the _user_ with the specified `id` is not found:

  - respond with HTTP status code `404` (Not Found).
  - return the following JSON object: `{ message: "The user with the specified ID does not exist." }`.

- If there's an error in retrieving the _user_ from the database:
  - respond with HTTP status code `500`.
  - return the following JSON object: `{ errorMessage: "The user information could not be retrieved." }`.

When the client makes a `DELETE` request to `/api/users/:id`:

- If the _user_ with the specified `id` is not found:

  - respond with HTTP status code `404` (Not Found).
  - return the following JSON object: `{ message: "The user with the specified ID does not exist." }`.

- If there's an error in removing the _user_ from the database:
  - respond with HTTP status code `500`.
  - return the following JSON object: `{ errorMessage: "The user could not be removed" }`.

When the client makes a `PUT` request to `/api/users/:id`:

- If the _user_ with the specified `id` is not found:

  - respond with HTTP status code `404` (Not Found).
  - return the following JSON object: `{ message: "The user with the specified ID does not exist." }`.

- If the request body is missing the `name` or `bio` property:

  - respond with HTTP status code `400` (Bad Request).
  - return the following JSON response: `{ errorMessage: "Please provide name and bio for the user." }`.

- If there's an error when updating the _user_:

  - respond with HTTP status code `500`.
  - return the following JSON object: `{ errorMessage: "The user information could not be modified." }`.

- If the user is found and the new information is valid:

  - update the user document in the database using the new information sent in the `request body`.
  - respond with HTTP status code `200` (OK).
  - return the newly updated _user document_.

PROGRESSION 4: BONUS

To work on the stretch problems you'll need to enable the cors middleware. Follow these steps:

  • add the cors npm module: npm i cors.
  • add server.use(cors()) after server.use(express.json()).

Submission

If you didn't add, commit and push the changes you made, this is the last call. 😄

please share your github links with your Mentors. Your Mentor's will check up your work and provide feedback.

Summary

If you managed to do it, good job! 🏆

We are proud of you!

Happy Coding ProGrad ❤️!