

Revisit the foodRoutes.js file and add the following lines of code between app.patch and module.The findOneAndUpdate() function in Mongoose has a wide variety of use cases. Step 7 - Building the Delete Routeįinally, you will build the functionality to remove an existing food item and save the changes to the database. The Postman results will display the object with modified calories. connect ( ", Īfter sending the Update Food request, send the Read All Food request again. Server.js const express = require ( "express" ) const mongoose = require ( "mongoose" ) const foodRouter = require ( "./routes/foodRoutes.js" ) const app = express ( ) In this section, you will create a new file to run the Express server, connect to the MongoDB Atlas database, and import future routes.Ĭreate a new server.js file and add the following lines of code:

In this section, you will create a directory for your project and install dependencies.Īt this point, you can initialize a new npm project: Once you have set a cluster, a database user, and an IP address you will be prepared for later acquiring the connection string as you set up the rest of your project. This project also requires a MongoDB Atlas account.Īfter creating an account and signing in, follow these steps to deploy a free tier cluster. This tutorial was verified with Node v15.3.0, npm v7.4.0, express v4.17.1, mongoose v5.11.12, and MongoDB v4.2. You can consult this article on routing with Express and this article on async and await.ĭownloading and installing a tool like Postman is recommended for testing API endpoints. Familiarity with setting up REST API without any authentication.

You can consult this article on Express and the official Express documentation.
