Session 23 - 24-REST API With Express, Mongo and Mongoose
Session 23 - 24-REST API With Express, Mongo and Mongoose
Mongoose
Objectives
◆ Develop a full-fledged REST API server with Express, MongoDB and
Mongoose
◆ Serve up various REST API end points together with interaction with the
MongoDB server.
03/06/2025 2
Full-Fledged REST API Server
ate
POST Cre
03/06/2025 3
HTTP Request to Database Operation Mapping
◆ Every incoming request needs to be decoded to decide the nature
of the request:
GET, PUT, POST, DELETE
Resource affected
Data in body of request
03/06/2025 4
Express Router + MongoDB + Mongoose
) Dishes.find({},
.post ( );
Dishes.create(
)
... )
03/06/2025 5
Update the Express Application
Update the Express Application
◆ Go to the conFusionServer folder where you had developed the
REST API server using Express generator.
◆ Copy the models folder from the node-mongoose folder to the
conFusionServer folder.
◆ Then install bluebird, mongoose and mongoose-currency Node
modules by typing the following at the prompt:
03/06/2025 7
Update the Express Application
◆ Open app.js file and add in the code to connect to the MongoDB
server as follows:
03/06/2025 8
Update the Express Application
◆ Next open dishes.js in the models folder and update it as follows:
next
03/06/2025 9
Update the Express Application
◆ Now open dishRouter.js and update its code as follows:
03/06/2025 10
Update the Express Application
03/06/2025 11
Run & Test REST API
◆ Save the changes and start the server. Make sure your MongoDB server is up and running.
◆ Use postman and then perform several operations on the REST API. You can manually insert
sample data into dishes collection like the following example
Sample a document
03/06/2025 12
Run & Test REST API
◆ Start the Server:
03/06/2025 13
Run & Test REST API Use Postman
◆ Get all
dishes:
03/06/2025 14
Run & Test REST API Use Postman
◆ Create a
new dish:
03/06/2025 15
Run & Test REST API Use Postman
◆ Get dish by
Id:
03/06/2025 16
Run & Test REST API Use Postman
◆ Update
dish by Id:
03/06/2025 17
Run & Test REST API Use Postman
◆ Delete dish
by Id:
◆ Get dish by Id
after delete:
03/06/2025 18
Accessing and updating comments within the dishes
◆ Add the following code to dishRouter.js to handle comments:
03/06/2025 19
dishRouter.js (contd)
03/06/2025 20
dishRouter.js (contd)
03/06/2025 21
dishRouter.js (contd)
03/06/2025 22
dishRouter.js (contd)
03/06/2025 23
dishRouter.js (contd)
03/06/2025 24
dishRouter.js (contd)
03/06/2025 25
Run & Test REST API
◆ Save the changes and start the server. Make sure your MongoDB server is
up and running.
◆ Use postman and then perform several operations on the REST API
◆ Start the Server:
03/06/2025 26
Summary
◆ Understand develop a full-fledged REST API server with Express,
MongoDB and Mongoose
◆ Understand serve up various REST API end points together with
interaction with the MongoDB server:
Create
Read
Update
Delete
03/06/2025 27