React + Vite + Node.js + MongoDB CRUD Tutorial (2024)
Pedro Machado / December 28, 2024
11 min read •
Description
Learn how to build a simple CRUD application with React, Vite, Node.js, and MongoDB using modern best practices as of 2024.
Getting Started with React + Vite + Node.js + MongoDB: A Step-by-Step Tutorial
Welcome to this comprehensive guide on building a full-stack CRUD application with React, Vite, Node.js, and MongoDB. In this tutorial, we’ll cover everything from setting up your project to integrating the front-end and back-end, creating database models, implementing CRUD operations, and testing your application. By the end, you’ll have a solid understanding of how to build and deploy a modern, performant, and scalable CRUD application.
(Prefer watching? Check out my YouTube channel Pedrotechnologies for more tutorials and in-depth explanations!)
Table of Contents
- Introduction
- Setting Up the Environment
- Backend Configuration
- Frontend Configuration
- Implementing CRUD in React
- Testing the Application
- Deployment and Best Practices
- Conclusion
1. Introduction
CRUD (Create, Read, Update, Delete) operations lie at the heart of almost every web application. In this tutorial, we’ll use the latest tools and best practices of 2024 to build a simple but complete full-stack application. Our tech stack:
- React (with Vite) for the front-end UI.
- Node.js + Express for the back-end server.
- MongoDB for the database.
Why This Stack?
- Scalability: Node.js excels at handling concurrent requests, making it a popular choice for modern web apps.
- Flexibility: React + Vite provides a fast, modular front-end environment with minimal configuration overhead.
- Performance: MongoDB’s document-based storage is both flexible and performant.
- Community and Ecosystem: Each component has a large ecosystem and active community, ensuring plenty of resources and support.
2. Setting Up the Environment
Prerequisites
Before proceeding, ensure you have the following installed on your machine:
- Node.js v18+ (for the latest ES features and better performance)
- npm, pnpm, or yarn (for installing dependencies)
- MongoDB (locally or via a cloud provider like MongoDB Atlas)
Installing Required Tools
- Node.js and npm: Download and install from nodejs.org.
- pnpm (recommended): Installation guide.
- MongoDB:
- Local: Install from mongodb.com.
- Cloud (MongoDB Atlas): Sign up and create a cluster.
3. Backend Configuration
Initializing the Node.js + Express Project
-
Create a new folder for your backend:
-
Initialize a package.json file:
-
Install Express and other dependencies:
- express: Popular Node.js framework for building APIs.
- cors: Enables cross-origin resource sharing between front-end and back-end.
- mongoose: ODM (Object Data Modeling) library for MongoDB.
- dotenv: Loads environment variables from a
.env
file.
-
Create a basic Express server:
index.js:
Connecting to MongoDB
Inside index.js, we can connect to our MongoDB instance using Mongoose:
Important: Create a
.env
file in the root of yourserver
directory and provide your MongoDB connection string:
(If using MongoDB Atlas, replace the local URI with your cluster’s URI.)
Creating the Data Model
Let’s assume we want to manage a simple User
resource. Create a file named User.js
in a new models
folder.
models/User.js:
Building the CRUD API Endpoints
Create a file named routes.js to handle your CRUD routes.
routes.js:
Finally, import and use these routes in your index.js:
This means all your routes will be prefixed with /api
. For example, GET /api/users
.
4. Frontend Configuration
Scaffolding the React + Vite Project
-
Create a new folder for your frontend:
-
Initialize a Vite + React project:
-
Follow the prompts and choose React. Then, install dependencies:
-
Project structure after creation:
client/ ├─ index.html ├─ package.json ├─ vite.config.js └─ src/ ├─ App.jsx ├─ main.jsx └─ ...
Project Structure
Here’s a suggested folder structure for the front end:
client/
├─ public/
├─ src/
│ ├─ components/
│ ├─ pages/
│ ├─ services/
│ ├─ App.jsx
│ └─ main.jsx
├─ package.json
└─ vite.config.js
Installing Additional Dependencies
You may want to install the following:
- axios: A promise-based HTTP client for making requests to our Node.js API.
- react-router-dom: Enables client-side routing in React applications.
5. Implementing CRUD in React
Fetching Data from the API
Open App.jsx (or create a dedicated Users.jsx component in pages/Users.jsx
) to fetch users from the API:
Creating New Records
Add a form to create a new user. For simplicity, we’ll place it in the same component:
Updating Existing Records
To update a user, you can add an edit button and form. One approach is to maintain an “edit mode” and a separate state for the user being edited:
Deleting Records
To delete a user:
6. Testing the Application
-
Start the Server:
-
Start the Frontend:
-
Open the App: Navigate to http://localhost:5173 (or the port provided by Vite).
-
Test CRUD:
- Create users via the form.
- View them in the list.
- Edit an existing user.
- Delete a user.
7. Deployment and Best Practices
Deployment Steps
-
Production Build (Front-End):
This generates a production-ready bundle in the
dist
folder. -
Configure the Backend to Serve Static Files (optional if you host separately).
-
Environment Variables: Use services like Heroku, Railway, or Render for hosting. Make sure to set your
MONGO_URI
andPORT
in their environment variable configurations. -
MongoDB Hosting: Use MongoDB Atlas for a managed, scalable database.
Security and Best Practices
- Use HTTPS in production.
- Sanitize inputs to prevent malicious data from entering your database.
- Use Access Control / Authentication (e.g., JWT) if you need protected routes.
- Use Logging (e.g., Morgan, Winston) for debugging and monitoring.
- Version Control: Keep your project in a Git repository (GitHub, GitLab, etc.).
8. Conclusion
Congratulations! You’ve built a complete CRUD application with React, Vite, Node.js, and MongoDB using up-to-date best practices for 2024. By applying this stack, you gain a solid foundation for building and scaling modern web applications. Here’s a quick recap:
- Backend: Node.js + Express + Mongoose for efficient, scalable APIs.
- Frontend: React + Vite for a speedy development and build process.
- CRUD: Create, Read, Update, and Delete user data end-to-end.
- Deployment: Steps to package and deploy your app in a production environment.
Feel free to expand this project with authentication, validation, or advanced features like pagination and search. If you found this tutorial helpful, be sure to subscribe to my YouTube channel Pedrotechnologies for more in-depth tutorials and demos.
Happy coding!