Topics:
- Databases
- MongoDB
- ORMs
- Mongoose
- Create/Read/Update/Delete operations
You'll write a server that lets you create and read Bears through MongoDB. Much of the knowledge from Node and Express will carry over to this mini project, where you'll interface with a database in your route handlers.
- Fork and Clone this project.
- Please head over to the Mongo Docs and Install MongoDB. You'll want to install the "Community Edition".
- To ensure you have Mongo installed properly, run
mongod
(to launch your mongo Daemon server) in your console. You should be able to connect at that point. - With your
mongod
server up and running, open a new terminal window and run mongo to see if you can connect with the mongo CLI repl and run the command >show dbs
. You should get some sort of output here that looks like
- To ensure you have Mongo installed properly, run
> show dbs
admin 0.000GB
auth 0.000GB
local 0.000GB
- This will probably difficult so please ask for help at this point if you need it.
cd
into your project directory.- Run
npm install
to download the dependencies. - Run
mongod --dbpath data
and keep it running in a separate terminal. - Write your implementation, as per the instructions below.
- To test your application at any point, run
npm start
to start the server. Then, you can make requests tohttp://localhost:3000
in Postman or in your browser! To make POST requests, you'll need to use Postman. Craft the correct requests to test your implementation!
Create a schema for the Bears collection. A schema is a description of the format of documents within a collection. In this case, each Bear is a document of the form:
{
species: "American Black Bear",
latinName: "Ursus americanus",
createdAt: Mon Aug 14 2017 12:50:16 GMT-0700 (PDT)
}
In models.js
, write the schema for the Bears collection. Make the field
createdAt
default to the current date.
When the client makes a POST
request to /bears
:
-
Ensure the client passes
species
andlatinName
parameters in the request body. If there's an error, respond with an appropriate status code, and send a JSON response of the form{ error: "Some useful error message" }
. -
Create and save a new Bear document. If there's an error while saving, send that error as a JSON response, and set the status code to
STATUS_SERVER_ERROR
(Internal Server Error). -
Otherwise, if everything is successful, send the Bear document as a JSON response.
When the client makes a GET
request to /bears
, read all the Bear documents
from MongoDB as an array. Send that array as a JSON response to the client.
If there's an error in retrieving the documents, send that error to the client
in a JSON response. Set the status code to STATUS_SERVER_ERROR
(Internal
Server Error), as the server couldn't fetch the documents for some reason.
When the client makes a GET
request to /bears/:id
(remember, :id
is a
parameter embedded in the URL, not in the query-string):
-
Find the Bear document associated with the given
id
. If there's an error, send that error as a JSON response, and set the status code toSTATUS_SERVER_ERROR
(Internal Server Error). -
Otherwise, if everything is successful, send the Bear document as a JSON response.