0% found this document useful (0 votes)
11 views27 pages

Session 23 - 24-REST API With Express, Mongo and Mongoose

The document outlines the development of a REST API server using Express, MongoDB, and Mongoose. It details the steps for setting up the server, mapping HTTP requests to database operations, and testing the API using Postman. Key functionalities include creating, reading, updating, and deleting resources in the MongoDB database.

Uploaded by

nguyenldhe176088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views27 pages

Session 23 - 24-REST API With Express, Mongo and Mongoose

The document outlines the development of a REST API server using Express, MongoDB, and Mongoose. It details the steps for setting up the server, mapping HTTP requests to database operations, and testing the API using Postman. Key functionalities include creating, reading, updating, and deleting resources in the MongoDB database.

Uploaded by

nguyenldhe176088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

REST API with Express, Mongo and

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

GET Query Database

ate
POST Cre

Server REST API

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

◆ Translate request to an equivalent database operation

03/06/2025 4
Express Router + MongoDB + Mongoose

Router().route(‘/uri’) Mongoose Schema


.get( + MongoDB

) 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:

npm install mongoose mongoose-currency --legacy-peer-deps

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

You might also like