- Databases
- MongoDB
- ODMs
- Mongoose
- Performing CRUD operations.
We will use Node.js and Express to write an API that can perform CRUD (Create, Read, Update and Destroy) operations on Bears stored in a MongoDB Database.
For this project you need to have MongoDB Community Edition installed and running. Having a local instance of MongoDB running on your system is the preferred option.
Alternatively, you can sign up for an account from a Database As A Service provider like MongoDB Atlas or mlab. Both DBAAS providers offer a free tier with 500MB size limit that can be used for development and testing.
If you don't have MongoDB installed, please click on this link for instructions on how to install and run the Community Server and the mongo shell. Follow the instructions for your Operating System.
After MongoDB is installed, follow the instructions on the documentation to start the server. Then run the mongo shell from a separate terminal and execute the show dbs
command. If all goes well you should see a list of available databases, similar to the sample below.
> show dbs
admin 0.000GB
local 0.000GB
- 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
. - After all dependencies finish downloading without errors, type
yarn start
ornpm start
to start the server.
- Make a GET Request to http://localhost:5005 using Postman. The response should be the following JSON object:
{
"status": "API running"
}
Use yarn or npm to add mongoose to to the project.
Inside server.js
, require mongoose and use it to connect your API to the BearKeeper
database in your MongoDB Server (local or remote). If the BearKeeper
database does not exist, it will be created automatically by MongoDB.
When the connection to MongoDB succeeds, log the following message to the console: "Successfully Connected to MongoDB".
If there is an error connecting to the database, log the following message to the console: "Database connection failed".
In a separate file, create a Schema for the Bears collection. Each Bear document should conform to the following object structure:
{
species: "American Black Bear", // String, required
latinName: "Ursus americanus", // String, required
createdOn: Mon Aug 14 2017 12:50:16 GMT-0700 (PDT) // Date, required, defaults to current date
}
Use mongoose to generate a Bear model that can be used to perform operations on Bear Documents. Remember to export the model to make it available for importing into other parts of the application.
Configure the API to respond to the following routes:
Method | Endpoint | Description |
---|---|---|
POST | /api/bears | Creates a bear using the information sent inside the request body . |
GET | /api/bears | Returns an array of all the bear objects contained in the database. |
GET | /api/bears/:id | Returns the bear object with the specified id. |
DELETE | /api/bears/:id | Removes the bear with the specified id and returns the deleted bear. |
PUT | /api/bears/:id | Updates the bear 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/bears
:
-
If the request body is missing the
species
orlatinName
property:- cancel the request.
- respond with HTTP status code
400
(Bad Request). - return the following JSON response:
{ errorMessage: "Please provide both species and latinName for the Bear." }
.
-
If the information about the Bear is valid:
- save the new Bear the the database.
- return HTTP status code
201
(Created). - return the newly created Bear Document.
-
If there's an error while saving the Bear:
- cancel the request.
- respond with HTTP status code
500
(Server Error). - return the following JSON object:
{ error: "There was an error while saving the Bear to the Database" }
.
When the client makes a GET
request to /api/bears
:
- If there's an error in retrieving the Bears 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/bears/:id
:
-
If the Bear with the specified
id
is not found:- return HTTP status code
404
(Not Found). - return the following JSON object:
{ message: "The Bear with the specified ID does not exist." }
.
- return HTTP status code
-
If there's an error in retrieving the Bear 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/bears/:id
:
-
If the Bear with the specified
id
is not found:- return HTTP status code
404
(Not Found). - return the following JSON object:
{ message: "The Bear with the specified ID does not exist." }
.
- return HTTP status code
-
If there's an error in removing the Bear from the database:
- cancel the request.
- respond with HTTP status code
500
. - return the following JSON object:
{ error: "The Bear could not be removed" }
.
When the client makes a PUT
request to /api/bears/:id
:
-
If the Bear with the specified
id
is not found:- return HTTP status code
404
(Not Found). - return the following JSON object:
{ message: "The Bear with the specified ID does not exist." }
.
- return HTTP status code
-
If there's an error when updating the Bear:
- cancel the request.
- respond with HTTP status code
500
. - return the following JSON object:
{ error: "The Bear information could not be modified." }
.
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.