MERN STACK CRUD tutorial with examples
The MERN stack is another popular full-stack web development framework consisting of MongoDB, Express, React, and Node.js. This stack allows you to build dynamic, single-page applications (SPAs) using JavaScript for both the front-end and back-end.
In this tutorial, we’ll build a simple MERN stack CRUD (Create, Read, Update, Delete) application that allows users to manage a list of tasks.
Prerequisites:
- Node.js and npm installed on your machine.
- React and Node.js basics.
- MongoDB installed locally or using a service like MongoDB Atlas.
- Basic understanding of JavaScript, React, and Express.
Step 1: Setting Up the Backend (Node.js + Express + MongoDB)
1.1. Initialize a new Node.js Project
First, create a new directory for your MERN stack app.
Initialize a Node.js project by running:
1.2. Install Dependencies
We need the following dependencies:
- express: For creating the server.
- mongoose: For MongoDB object modeling.
- cors: To allow cross-origin requests between client and server.
- dotenv: To manage environment variables securely.
- body-parser: To parse incoming request bodies.
Run the following command to install the necessary packages:
1.3. Create the Backend Server
Create an index.js
file in the root of your project. This file will set up the Express server, MongoDB connection, and define routes for CRUD operations.
1.4. Create .env
File for Environment Variables
In the root of your project, create a .env
file to store your MongoDB URI securely.
Note: If you're using MongoDB Atlas, replace the
MONGODB_URI
value with your Atlas connection string.
1.5. Run the Backend Server
To start the server, run the following command:
The server will be running on http://localhost:5000.
Step 2: Setting Up the Frontend (React)
2.1. Create a React App
Navigate back to your root directory and create a React app in the client
folder using the Create React App tool.
2.2. Install Axios for HTTP Requests
Axios is a promise-based HTTP client that we will use to make API requests to the backend.
2.3. Create a Task Component to Display and Manage Tasks
In the src
folder, create a new component called TaskList.js
.
2.4. Set Up React App to Use the Task Component
In src/App.js
, import and use the TaskList
component.
2.5. Running the React Frontend
To run the React app, use the following command in the client
folder:
The React app will be running on http://localhost:3000.
Step 3: Testing the MERN CRUD Application
- Create a Task: Enter a task name and click Add Task. The task will be added to the backend and appear in the task list.
- Update a Task: Click on a task name to toggle its completed status (cross out the name if completed).
- Delete a Task: Click the Delete button next to a task to delete it from both the frontend and backend.
- View Tasks: All tasks will be displayed in the task list. They will be dynamically fetched from the backend each time the page reloads.
Conclusion
In this tutorial, we built a simple MERN stack CRUD application to manage tasks. We used Node.js and Express for the backend, MongoDB for the database, and React for the frontend.
You can extend this application by adding additional features such as user authentication, task prioritization, due dates, etc. You could also deploy the application using platforms like Heroku or Vercel for the frontend, and MongoDB Atlas for the database.
Comments
Post a Comment