Goal: Build a bird sighting API in Express using four common CRUD operations in MongoDB using MongoJS.
Note the following sample data, for sightings:
[{
"name": "red breasted merganser",
"order": "Anseriformes",
"status": "least concern"
},
{
"name": "cedar waxwing",
"order": "Passeriformes",
"status": "least concern"
},
{
"name": "osprey",
"order": "Accipitriformes",
"status": "extinct"
},
{
"name": "snowy plover",
"order": "Charadriformes",
"status": "near threatened"
}]
Create your Express.js app by cloning this repository.
npm install
Start your application by running:
node server.js
Test each of your endpoints in Postman with the following URLs:
- POST
/api/sighting
- GET
/api/sighting?region=america
- PUT
/api/sighting?id=some-id
- DELETE
/api/sighting?id=some-id
If everything's working, you'll see console output each time you hit your endpoints.
Start the mongo daemon in a separate terminal window.
Now, require the MongoJS module, and create a database by connecting to it in server.js
. Name your database birds
.
HINT: Read the documentation
Create a var that references a collection called sightings
.
Upgrade your POST endpoint with code to create a sighting document from the body
of the request. The body should include name
, order
, and status
.
For steps 7 through 10, test each of your endpoints again. Note the sample data above for your tests.
Modify the GET endpoint to retrieve all sightings with a given status
, as stated in the request query.
Update your PUT endpoint to accept a body
modifying an existing sighting's order
field. Use the id
parameter in the query string to identify the sighting to change.
Update your DELETE endpoint to delete a sighting document by id
in the query string.