- Databases.
- MongoDB.
- Mongoose.
- Performing CRUD Operations.
Use Node.js, Express.js and Mongoose.js to build an API that persists data to a MongoDB database.
- Fork and Clone this repository.
- CD into the folder where you cloned the repository.
- Type
yarn
ornpm install
to download all dependencies listed insidepackage.json
. - Add code inside
server.js
and any other files you create in order to implement your API. - To start the server, type
yarn start
ornpm start
. The server will restart automatically as you make changes. - Use Postman to Test the API.
In a separate file, create a Schema for the friends collection. Each friend document should conform to the following object structure:
{
firstName: "Jane", // String, required
lastName: "Doe", // String, required
age: 18, // Number, required, should be an integer between 1 and 120
createdOn: Mon Aug 14 2017 12:50:16 GMT-0700 (PDT) // Date, required, defaults to current date
}
Configure the API to respond to the following routes:
Method | Endpoint | Description |
---|---|---|
POST | /api/friends | Creates a friend using the information sent inside the request body . |
GET | /api/friends | Returns an array of all the friend objects contained in the database. |
GET | /api/friends/:id | Returns the friend object with the specified id. |
DELETE | /api/friends/:id | Removes the friend with the specified id and returns the deleted friend. |
PUT | /api/friends/:id | Updates the friend with the specified id using data from the request body . Returns the modified document, NOT the original. |
When the client makes a POST
request to /api/friends
:
-
If the request body is missing the
firstName
,lastName
orage
property:- cancel the request.
- respond with HTTP status code
400
(Bad Request). - return the following JSON response:
{ errorMessage: "Please provide firstName, lastName and age for the friend." }
.
-
If the value for
age
is not an integer or is less than 1 or more than 120:- cancel the request.
- respond with HTTP status code
400
(Bad Request). - return the following JSON response:
{ errorMessage: "Age must be a whole number between 1 and 120" }
.
-
If the information about the friend is valid:
- save the new friend the the database.
- return HTTP status code
201
(Created). - return the newly created friend document.
-
If there's an error while saving the friend:
- cancel the request.
- respond with HTTP status code
500
(Server Error). - return the following JSON object:
{ error: "There was an error while saving the friend to the database" }
.
When the client makes a GET
request to /api/friends
:
- If there's an error in retrieving the friends from the database:
- cancel the request.
- respond with HTTP status code
500
. - return the following JSON object:
{ error: "The information could not be retrieved." }
.
When the client makes a GET
request to /api/friends/:id
:
-
If the friend with the specified
id
is not found:- return HTTP status code
404
(Not Found). - return the following JSON object:
{ message: "The friend with the specified ID does not exist." }
.
- return HTTP status code
-
If there's an error in retrieving the friend from the database:
- cancel the request.
- respond with HTTP status code
500
. - return the following JSON object:
{ error: "The information could not be retrieved." }
.
When the client makes a DELETE
request to /api/friends/:id
:
-
If the friend with the specified
id
is not found:- return HTTP status code
404
(Not Found). - return the following JSON object:
{ message: "The friend with the specified ID does not exist." }
.
- return HTTP status code
-
If there's an error in removing the friend from the database:
- cancel the request.
- respond with HTTP status code
500
. - return the following JSON object:
{ error: "The friend could not be removed" }
.
When the client makes a PUT
request to /api/friends/:id
:
-
If the friend with the specified
id
is not found:- return HTTP status code
404
(Not Found). - return the following JSON object:
{ message: "The friend with the specified ID does not exist." }
.
- return HTTP status code
-
If the request body is missing the
firstName
,lastName
orage
property:- cancel the request.
- respond with HTTP status code
400
(Bad Request). - return the following JSON response:
{ errorMessage: "Please provide firstName, lastName and age for the friend." }
.
-
If the value for
age
is not an integer or is less than 1 or more than 120:- cancel the request.
- respond with HTTP status code
400
(Bad Request). - return the following JSON response:
{ errorMessage: "Age must be a whole number between 1 and 120" }
.
-
If there's an error when updating the friend:
- cancel the request.
- respond with HTTP status code
500
. - return the following JSON object:
{ error: "The friend information could not be modified." }
.
-
If the friend is found and the new information is valid:
- update the friend document in the database using the new information sent in the
reques body
. - return HTTP status code
200
(OK). - return the newly updated friend document.
- update the friend document in the database using the new information sent in the
Remember to use body-parser
to read information from the request body.
Stop the MongoDB database server when not in use to save computer resources.
- Implement a second collection called
BlogPosts
. - Define the schema however you see fit.
- Implement all CRUD operations.
- Add validation.