In this exercise, you will learn how to create REST api end points:
- Building a RESTful API.
- Performing CRUD operations.
- Writing API endpoints.
- Fork this repo
- Clone this repo
Whenever you create a first significant change, you should make your first commit.
- 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.
Use Node.js and Express to build an API that performs CRUD operations on users.
- Add a
.gitignore
file appropriate fornode.js
projects. - Add a new
package.json
. - Add a
server
script topackage.json
that runs the API usingnodemon
.
Your task in this iteration is to create a
EXPRESS
server.- An account in
Mongo Atlas
- connection between node and atlas using mongoose.
Create a prograd user model class schema
{
// hint: use the shortid npm package to generate it
name: "Pro Grad", // String, required
email: "[email protected]", // String, required
age: 3,
prograd_id: 001,
squad: 64
}
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 |
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_.
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())
afterserver.use(express.json())
.
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.
If you managed to do it, good job! ๐
We are proud of you!
Happy Coding ProGrad โค๏ธ!