Sample api to return basic information about Formula 1.
- Express -> Framewrok for Node js to create CRUD api's.
- Mongoose -> Library to manage data models based on schemas.
- Nodemonn -> Tool to restart Node application when saving file changes
- body-parser -> Middleware to parse request bodies
Instead of creating the DB locally, we can use Mongo DB Atlas. It is a free service to use and be able to create a DataBase in the cloud. On the other hand, we have Mongo DB, this is the batabase by itself, where we store all the information.
Steps to add Mongo to our project:
-
Install mongo in the project
npm install mongodb --save
. The flag --save is to install in the porject and updates the version in the package.json. -
Get an instance of Mongo client
const MongoClient = require('mongodb').MongoClient
-
Conect to the database .
MongoClient.connect('mongodb-connection-string', (err, client) => {
// ... do something here
})
-
Create the project in Mongo DB Atlas.
-
Get the
mongodb-connection-string
that will help us to connect to the db in atlas.
For this, I have used the extension REST Client for Visual Studio Code. With that extension you just have to create a new file called whatEver.rest
and then, you can specify the different endpoints and check if it is working correctly.
Here is all the documentation
- app.js -> Contains the connection to mongoose and all the configuration for the connection.
- routes -> Contains all the routes for our api. It will be diferentiated by collections.
- controller -> Conatins all the logic to resolve the different operations (Create, Read, Update, Delete)
- models -> Contains all the Schemas that it will be used for the different operations.
Create a CRUD api with Express and Mongo
https://zellwk.com/blog/crud-express-mongodb/
All Mongo db operations