Hello! Today, we’ll be setting up MongoDB and Express.js to build a web application. By the end of this lesson, you’ll be able to set up these tools and connect them, which is essential for creating web applications that can store and manage data.
In this lesson, you'll learn:
- What MongoDB is and why it is important.
- How to install and set up MongoDB.
- How to set up Express.js.
- How to connect MongoDB with Express.js.
- How to perform CRUD operations using MongoDB and Express.js.
- How to run a simple Express server.
MongoDB
is a NoSQL database designed to store large amounts of data in a flexible, JSON-like format called BSON
(Binary JSON). Unlike traditional SQL databases that store data in tables with rows and columns, MongoDB
stores data in documents and collections, making it highly scalable and flexible. This flexibility is particularly useful in real-life applications like e-commerce, where product attributes can vary significantly between items.
Although MongoDB is pre-installed on CodeSignal, it's useful to know how to install it on your own computer.
- Download the MongoDB Community Server suitable for your operating system.
- Follow the installation instructions specific to your OS.
- After installation, open your terminal and type
mongod
to start the MongoDB server.
Example:
Shell Session1mongod
This command starts the MongoDB server, ensuring it is running correctly.
Let's create an Express server and connect it to MongoDB
, which is helpful for setting up our database and web server to interact with each other.
JavaScript1const express = require('express'); 2const mongoose = require('mongoose'); 3 4const app = express(); 5const PORT = 3000; 6 7// Set mongoose strictQuery to true to suppress deprecation warning 8mongoose.set('strictQuery', true); 9 10// Connect to MongoDB 11mongoose.connect('mongodb://127.0.0.1:27017/todo-app', { 12 useNewUrlParser: true, // Use the new URL parser instead of the deprecated one 13 useUnifiedTopology: true // Use the new Server Discover and Monitoring engine 14}) 15.then(() => console.log("Connected to MongoDB")) 16.catch((error) => console.error("Failed to connect to MongoDB:", error)); 17 18app.use(express.json()); 19 20app.get('/', (req, res) => { 21 res.send('Hello, world!'); 22}); 23 24app.listen(PORT, () => { 25 console.log(`Server running on http://localhost:${PORT}`); 26});
This code sets up a basic Express.js server and connects to a MongoDB
database named todo-app
running locally. The express.json()
middleware is used to parse incoming JSON requests. The server listens on port 3000 and sends a "Hello, world!" message when accessed at the root URL.
Let's understand the Mongoose connection options better:
useNewUrlParser
: This option tells Mongoose to use the new MongoDB connection string parser to avoid connection issues related to the deprecated parser.useUnifiedTopology
: This option enables the new Server Discover and Monitoring engine, which helps with handling connection pooling and server discovery in a more robust and efficient manner.
In this lesson, we learned about MongoDB
, a flexible and scalable NoSQL database, and how it differs from traditional SQL databases. We also covered the installation process for MongoDB and how to set it up on your local machine. Moving forward, we set up an Express.js server and connected our Express application to a MongoDB database using Mongoose
. Finally, we ran a simple Express server that interacts with MongoDB, laying the foundation for performing CRUD operations. Practice these steps to build a solid understanding and confidence in using these tools. Happy coding!