A boilerplate for building full-stack applications using the MERN (MongoDB, Express, React, Node.js) stack. This template sets up essential configurations and file structures, allowing you to quickly start developing robust, scalable web applications.
- Express Backend: RESTful API setup with environment-specific configurations.
- React Frontend: Vite-powered development with hot-reloading.
- MongoDB Integration: Simple database connection using MongoDB Atlas or a local MongoDB server.
- Environment Management:
.env
support for flexible configuration in development and production.
Follow the steps below to set up and run the project locally.
Ensure the following tools are installed on your machine:
- Node.js (v20.x or later)
- MongoDB Community Server or MongoDB Atlas (for remote hosting)
- Nodemon (for backend hot-reloading)
- MongoDB Compass (optional, for database inspection)
/mern-template
├── /backend # Express server-side code
└── /frontend # React client-side code
-
Navigate to the backend directory:
cd backend
-
Install dependencies:
npm install
-
Set up environment variables:
Create a
.env
file in thebackend
directory with the following keys:PORT=5004 CORS_ORIGINS=http://localhost:5173 TOKEN_SECRET=YOUR_TOKEN_SECRET CONNECTION_URI=YOUR_CLUSTER_URI STRIPE_SECRET_KEY=YOUR_STRIPE_SECRET_KEY
-
Run the development server:
npm run dev
The backend API will now be running at
http://localhost:5004
.
npm run start
– Starts the server in production mode.npm run dev
– Starts the server in development mode with Nodemon for auto-restarts on changes.
-
Navigate to the frontend directory:
cd frontend
-
Install dependencies:
npm install
-
Set up environment variables:
Create
.env
and.env.production
files in thefrontend
directory:# .env (for development) VITE_API_URL=http://localhost:5004/api # .env.production (for production) VITE_API_URL=/api
-
Run the React development server:
npm run dev
The frontend will be available at
http://localhost:5173
.
npm run dev
– Starts the frontend development server with hot-reloading.npm run build
– Builds the frontend for production.
-
Start the backend server:
cd backend npm run dev
-
Start the frontend server:
cd frontend npm run dev
Visit http://localhost:5173
to view the app. The frontend will proxy API requests to the backend server running on http://localhost:5004
.
-
Backend Environment Variables:
PORT
– Port for the backend server (default: 5004).TOKEN_SECRET
– Secret key for JWT authentication.CONNECTION_URI
– MongoDB connection string.
-
Frontend Environment Variables:
VITE_API_URL
– API base URL for both development and production environments.
This project is licensed under the MIT License. See the LICENSE file for more details.