Skip to content

Backend for MERN stack application using Node.js, Express.js, and MongoDB. Handles API routes, authentication, and database operations.

Notifications You must be signed in to change notification settings

kinshukkush/MERN-BACKEND

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”§ MERN Store - Backend API

RESTful API for MERN Store e-commerce platform with JWT authentication, role-based access control, and MongoDB database.

Node.js MongoDB License

πŸš€ Features

  • πŸ” JWT authentication and authorization
  • πŸ‘₯ User management (CRUD operations)
  • πŸ“¦ Product management
  • πŸ›’ Order processing and tracking
  • πŸ”’ Role-based access control (Admin/User)
  • 🌐 CORS enabled for frontend integration
  • ☁️ MongoDB Atlas cloud database
  • πŸš€ Deployed on Vercel serverless functions

πŸ› οΈ Tech Stack

  • Node.js - Runtime environment
  • Express.js - Web framework
  • MongoDB Atlas - Cloud database
  • Mongoose - ODM for MongoDB
  • JWT (jsonwebtoken) - Authentication
  • Bcrypt - Password hashing
  • CORS - Cross-origin resource sharing

πŸ“‹ Prerequisites

  • Node.js (v18 or higher)
  • npm or yarn
  • MongoDB Atlas account (or local MongoDB)

πŸš€ Installation & Setup

1. Clone the Repository

git clone https://github.com/kinshukkush/mern-backend-main.git
cd mern-backend-main

2. Install Dependencies

npm install

3. Environment Configuration

Create a .env file in the root directory:

# MongoDB Configuration
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/database?retryWrites=true&w=majority

# Server Configuration
PORT=8080

# JWT Secret (use a strong, random string)
JWT_SECRET=your_secure_jwt_secret_key_here

# Environment
NODE_ENV=development

Important: Replace the MongoDB URI with your actual connection string from MongoDB Atlas.

4. Start the Server

# Start with Node
node server.js

# Or with nodemon (for development)
nodemon server.js

The API will be available at: http://localhost:8080

You should see:

βœ… MongoDB connected successfully!
πŸš€ Server running at http://localhost:8080

πŸ“ Project Structure

β”œβ”€β”€ controllers/              # Route controllers
β”‚   β”œβ”€β”€ orderController.js   # Order operations
β”‚   β”œβ”€β”€ productController.js # Product operations
β”‚   └── userController.js    # User operations
β”œβ”€β”€ middlewares/             # Custom middleware
β”‚   └── auth.js             # Authentication & authorization
β”œβ”€β”€ models/                  # Mongoose schemas
β”‚   β”œβ”€β”€ orderModel.js       # Order schema
β”‚   β”œβ”€β”€ productModel.js     # Product schema
β”‚   └── userModel.js        # User schema
β”œβ”€β”€ routes/                  # API routes
β”‚   β”œβ”€β”€ orderRoute.js       # Order routes
β”‚   β”œβ”€β”€ productRoute.js     # Product routes
β”‚   └── userRoute.js        # User routes
β”œβ”€β”€ public/                  # Static files
β”œβ”€β”€ server.js               # Main server file
β”œβ”€β”€ vercel.json             # Vercel deployment config
└── .env                    # Environment variables (not in git)

πŸ”Œ API Endpoints

Base URL

http://localhost:8080/api

Authentication Routes

Method Endpoint Description Auth Required
POST /users/register Register new user No
POST /users/login User login No

Example Request (Login):

POST /api/users/login
{
  "email": "user@example.com",
  "password": "password123"
}

User Routes

Method Endpoint Description Auth Required
GET /users Get all users Admin
GET /users/:id Get user by ID Yes
GET /users/:id/profile Get user profile Yes
PATCH /users/:id/profile Update profile Yes
DELETE /users/:id Delete user Admin

Product Routes

Method Endpoint Description Auth Required
GET /products/all Get all products (public) No
GET /products Get products (paginated) Admin
GET /products/:id Get product by ID No
POST /products Create product Admin
PATCH /products/:id Update product Admin
DELETE /products/:id Delete product Admin

Order Routes

Method Endpoint Description Auth Required
POST /orders Create new order Yes
GET /orders Get all orders Admin
GET /orders/:email Get orders by user email Yes
GET /orders/:id Get order by ID Yes
PATCH /orders/:id Update order status Admin
DELETE /orders/:id Delete order Admin

πŸ”’ Authentication

Protected routes require JWT token in the Authorization header:

Authorization: Bearer <your_jwt_token>

Example:

headers: {
  'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
  'Content-Type': 'application/json'
}

User Roles

  • User - Can view products, create orders, manage own profile
  • Admin - Full access to all resources (users, products, orders)

🌐 Environment Variables

Variable Description Required
MONGODB_URI MongoDB connection string Yes
JWT_SECRET Secret key for JWT signing Yes
PORT Server port No (default: 8080)
NODE_ENV Environment mode No (default: development)

πŸš€ Deployment

Vercel Deployment

  1. Push your code to GitHub
  2. Import the project in Vercel
  3. Set environment variables in Vercel dashboard:
    • MONGODB_URI
    • JWT_SECRET
    • NODE_ENV=production
    • PORT=8080
  4. Deploy

Live API: https://mern-backend-main-zeta.vercel.app/

CORS Configuration

The API allows requests from:

  • http://localhost:5174 (Local development)
  • https://mern-frontent-main.vercel.app (Production frontend)

Update CORS origins in server.js if your frontend URL changes.

πŸ”— Related Repositories

πŸ“œ Available Scripts

# Start server
node server.js

# Start with auto-reload (requires nodemon)
nodemon server.js

# Install dependencies
npm install

πŸ› Common Issues

MongoDB Connection Error

  • Check your MongoDB URI is correct
  • Ensure your IP is whitelisted in MongoDB Atlas
  • Verify network access settings

Port Already in Use

# Windows
netstat -ano | findstr :8080
taskkill /PID <PID> /F

# Linux/Mac
lsof -ti:8080 | xargs kill -9

CORS Errors

  • Verify frontend URL is in CORS origins
  • Check VITE_API_URL in frontend .env

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License.

πŸ‘¨β€πŸ’» Author

Kinshuk Saxena

πŸ™ Acknowledgments

  • MongoDB team for the excellent database
  • Express.js for the minimal web framework
  • JWT for secure authentication

Made with ❀️ using Node.js and MERN Stack

About

Backend for MERN stack application using Node.js, Express.js, and MongoDB. Handles API routes, authentication, and database operations.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published