This project is a MERN (MongoDB, Express, React, Node.js) stack application built from scratch by following a practice tutorial. The main goal is to learn and reinforce CRUD operations with MongoDB and Express, as well as basic front-end functionality with React. The code is hand-written while following the referenced video to understand each step of the MERN architecture.
Currently, the project demonstrates:
- A simple Express server to handle API requests.
- Integration with MongoDB for data storage (via Mongoose).
- A React front end to interact with the API.
- MongoDB: NoSQL database for storing application data.
- Mongoose: ODM (Object Data Modeling) library for MongoDB.
- Node.js: JavaScript runtime environment for the server side.
- Express: Framework for building RESTful APIs on Node.js.
- React: JavaScript library for building the user interface.
- JavaScript (ES6+): Main programming language across the stack.
Note: For detailed instructions on setting up and using MongoDB, refer to the MongoDB Documentation.
The repository is divided into multiple folders for organizational clarity:
-
server(orbackend):models: Contains Mongoose models representing database entities.routes(orcontrollers): Defines the Express routes/endpoints for CRUD operations.config: May contain database connection settings or environment configuration.server.js(orindex.js): Entry point for the Node.js server.
-
client(orfrontend):src:components: Contains React components (e.g., forms, tables, lists).pages: Contains main application pages or views.App.js: Root component that manages routing and global layout.index.js: Entry point for the React application.
- Create: Allows adding new data entries (e.g., user, post, product, etc.) in the database.
- Read: Provides endpoints and UI components to retrieve and display data.
- Update: Enables updating existing records directly from the front end.
- Delete: Allows removing records from the database.
Depending on the tutorial or practice goals, these features may be adapted to specific data models and UI design.
- Defines a
Userschema with fields likename,email, andpassword. - Uses Mongoose to establish the schema and model.
- Handles requests such as
GET /users,POST /users,PUT /users/:id, andDELETE /users/:id. - Communicates with the
Usermodel to perform database operations.
- Sets up the Node.js server using Express.
- Connects to MongoDB using Mongoose.
- Listens on a specified port (e.g.,
localhost:5000).
- Main React component that sets up React Router (if applicable).
- Renders different pages or components based on the URL path.
- React component for creating or updating user data.
- Includes form fields and submission logic that calls the Express API.
You can test the API endpoints with Postman or Thunder Client, or by making requests directly from the React application:
-
Create a Record
- Method:
POST - URL:
http://localhost:5000/users(example) - Body (JSON):
{ "name": "Alice", "email": "alice@example.com", "password": "securepassword" } - Response: Returns the newly created object.
- Method:
-
Read All Records
- Method:
GET - URL:
http://localhost:5000/users - Response: Returns an array of user objects.
- Method:
-
Update a Record
- Method:
PUT - URL:
http://localhost:5000/users/123(where123is a user ID) - Body (JSON):
{ "name": "Alice Updated" } - Response: Returns the updated user object.
- Method:
-
Delete a Record
- Method:
DELETE - URL:
http://localhost:5000/users/123 - Response: Returns a confirmation message or the deleted user object.
- Method:
- Node.js (v14+ recommended)
- MongoDB account and/or local MongoDB server
- Basic familiarity with JavaScript and React
- Clone the Repository:
git clone <repository-url>
- Install Dependencies:
- Backend:
cd backend npm run dev - Frontend:
cd frontend npm run dev
- Backend:
- Configure the Database:
- In
server/configor.envfile, add your MongoDB connection string. - For detailed instructions, see the MongoDB Documentation.
- In
- Run the Backend:
By default, the server might run at
cd backend npm run devhttp://localhost:5000(depending on your setup). - Run the Frontend:
The React app typically runs at
cd frontend npm run devhttp://localhost:3000.
This code was created while following the tutorial available at:
React Node.js MERN Stack Tutorial