Migrating a web application to MongoDB
We already added MongoDB to our project using Docker Compose and npm commands, but we have not started using it yet. In this section, we will migrate a web application to MongoDB.
Installing dependencies
We will install the following dependencies:
npm install [email protected] dotenv@16
Managing the secrets
We will create a .env
file with the following content:
MONGODB_URI=mongodb://localhost:27017/whispering-database PORT=3000
Then, we will load the environment variables from the .env
file with the following code into index.js
:
import { app } from './server.js' import mongoose from 'mongoose' const port = process.env.PORT try { await mongoose.connect(process.env.MONGODB_URI); console.log('Connected to MongoDB') app.listen(port, () => { console.log(`Running in http://localhost:${port}`) }) } catch (error) { console...