A robust Node.js backend API for an e-commerce web application built with Express.js, MongoDB, and JWT authentication.
- User registration and login
- JWT-based authentication
- Role-based access control (Admin/General users)
- Secure password hashing with bcryptjs
- Cookie-based token management
- User profile management
- Admin panel for user management
- User details retrieval
- User logout functionality
- Product upload (Admin only)
- Product listing and retrieval
- Product updates (Admin only)
- Category-wise product filtering
- Product search functionality
- Product details view
- Add products to cart
- View cart items
- Update cart quantities
- Remove items from cart
- Cart item counting
- Runtime: Node.js
- Framework: Express.js
- Database: MongoDB with Mongoose ODM
- Authentication: JWT (JSON Web Tokens)
- Password Hashing: bcryptjs
- Environment Variables: dotenv
- CORS: Cross-Origin Resource Sharing enabled
- Testing: Jest
Before running this application, make sure you have the following installed:
- Node.js (v14 or higher)
- MongoDB (local instance or MongoDB Atlas)
- npm or yarn package manager
-
Clone the repository
git clone <repository-url> cd e-app-web-application-backend
-
Install dependencies
npm install
-
Environment Setup Create a
.envfile in the root directory and add the following variables:MONGODB_URI=mongodb://localhost:27017/your-database-name TOKEN_SECRET_KEY=your-jwt-secret-key FRONTEND_URL=http://localhost:3000 PORT=8080
-
Start the development server
npm run dev
-
Start the production server
npm start
{
name: String,
email: String (unique, required),
password: String,
profilePic: String,
role: String (ADMIN/GENERAL),
timestamps: true
}{
productName: String,
brandName: String,
category: String,
productImage: Array,
description: String,
price: Number,
sellingPrice: Number,
timestamps: true
}{
productId: String (ref: product),
quantity: Number,
userId: String,
timestamps: true
}POST /api/signup- User registrationPOST /api/signin- User loginGET /api/userLogout- User logoutGET /api/user-details- Get user details (Protected)
GET /api/all-user- Get all users (Admin only)POST /api/update-user- Update user details (Admin only)
POST /api/upload-product- Upload new product (Admin only)GET /api/get-product- Get all productsPOST /api/update-product- Update product (Admin only)GET /api/get-categoryProduct- Get product categoriesPOST /api/category-product- Get products by categoryPOST /api/product-details- Get product detailsGET /api/search- Search productsPOST /api/filter-product- Filter products
POST /api/addtocart- Add item to cart (Protected)GET /api/countAddToCartProduct- Get cart items count (Protected)GET /api/view-card-product- View cart items (Protected)POST /api/update-cart-product- Update cart item quantity (Protected)POST /api/delete-cart-product- Remove item from cart (Protected)
Run the test suite using Jest:
npm test├── config/
│ └── db.js # Database connection
├── controller/
│ ├── product/ # Product-related controllers
│ └── user/ # User-related controllers
├── helpers/
│ └── permission.js # Permission helper functions
├── middleware/
│ └── authToken.js # JWT authentication middleware
├── models/
│ ├── cartProduct.js # Cart model
│ ├── productModel.js # Product model
│ └── userModel.js # User model
├── routes/
│ └── index.js # API routes definition
├── index.js # Application entry point
└── package.json # Project dependencies
- JWT token-based authentication
- Password hashing with bcryptjs
- CORS configuration
- Role-based authorization
- HTTP-only cookies for token storage
npm run dev- Start development server with nodemonnpm start- Start production servernpm test- Run tests
The project follows standard JavaScript conventions with:
- Async/await for asynchronous operations
- Error handling with try-catch blocks
- Consistent response format for all API endpoints
Note: Make sure to configure your environment variables properly before running the application in production.