April 2020
Intermediate to advanced
716 pages
18h 55m
English
To connect your Node server to MongoDB, add the following code to server.js, and make sure you have MongoDB running in your workspace or you have the URL of a cloud MongoDB database instance:
mern-simplesetup/server/server.js:
import { MongoClient } from 'mongodb'const url = process.env.MONGODB_URI || 'mongodb://localhost:27017/mernSimpleSetup'MongoClient.connect(url, (err, db)=>{ console.log("Connected successfully to mongodb server") db.close()})
In this code example, MongoClient is the driver that connects to the running MongoDB instance using its URL. It allows us to implement the database-related code in the backend. This completes our full-stack integration for this simple web application using the MERN ...