A comprehensive Node.js e-commerce backend application built with Express.js and MongoDB, featuring user authentication, product management, shopping cart functionality, and owner administration.
- User Authentication: Secure registration and login with JWT tokens
- Product Browsing: Browse products with search, filter, and sort functionality
- Shopping Cart: Add/remove products from cart with quantity management
- Responsive Design: Mobile-friendly interface with Tailwind CSS
- Single Owner System: Secure owner account creation and management
- Product Management: Create products with images, pricing, and categories
- Admin Dashboard: Dedicated admin interface for product creation
- Password Hashing: bcrypt for secure password storage
- JWT Authentication: Token-based authentication with HTTP-only cookies
- Session Management: Express sessions with flash messaging
- Input Validation: Comprehensive input validation and sanitization
backend_e-commerce/
β
βββ config/
β βββ development.json # Development configuration
β βββ keys.js # Environment keys
β βββ mongooseConnection.js # Database connection setup
β βββ multerConfig.js # File upload configuration
βββ controllers/
β βββ authController.js # Authentication logic
βββ middlewares/
β βββ isLoggedIn.js # Authentication middleware
βββ models/
β βββ ownermodels.js # Owner schema
β βββ productmodels.js # Product schema
β βββ usermodels.js # User schema
βββ routes/
β βββ index.js # Main routes (shop, cart)
β βββ ownerRouter.js # Owner/admin routes
β βββ productRouter.js # Product management routes
β βββ userRouter.js # User authentication routes
βββ utils/
β βββ generateToken.js # JWT token generation utility
βββ public/
β βββ CSS/
β β βββ createProduct.css
β βββ JS/
β βββ script.js # Index page frontend functionality
β βββ shop.js # Shop page interactions
β βββ ownerDashbord.js # Owner's Dashbord page interactions
β βββ createProduct.js # createProduct page interactions
β βββ cart.js # Cart management
βββ views/
β βββ index.ejs # Home page templates
β βββ shop.ejs # Shop page templates
β βββ cart.ejs # Cart page templates
β βββ createProduct.ejs # owner's product creation page templates
β βββ ownerDashbord.ejs # owner's dashbord.js page templates
β βββ contact.ejs # Contact page templates
β βββ about.ejs # About page templates
β
βββ .env #Environment file
βββ .gitignore
βββ app.js # Main application entry point
βββ package.json # Dependencies and scripts
βββ package-lock.json
βββ README.md # Readme file
- Node.js (v14 or higher)
- MongoDB (running locally or remote connection)
- npm or yarn package manager
-
Clone the repository
git clone <repository-url> cd backend_e-commerce
-
Install dependencies
npm install
-
Environment Setup Create a
.envfile in the root directory:JWT_KEY=your-super-secret-jwt-key-change-in-production SESSION_SECRET=your-session-secret-key-here NODE_ENV=development
-
Database Configuration Ensure your
config/development.jsoncontains:{ "MONGODB_URL": "mongodb://127.0.0.1:27017" } -
Start MongoDB Make sure MongoDB is running on your system
-
Run the application
npm start
or for development with auto-reload:
nodemon app.js
-
Access the application Open your browser and navigate to
http://localhost:3000
{
fullName: String (required, min: 3 chars),
email: String (required, unique),
password: String (required, hashed),
location: String (required),
contactNo: Number (required),
cart: [ObjectId] (references Product),
wishlist: Array,
order: Array,
profileImage: String
}{
fullName: String (required, min: 3 chars),
email: String (required, unique),
password: String (required, hashed),
gstno: String (required),
products: Array,
profileImage: String
}{
name: String (required),
description: String,
rate: Number (default: 4.5),
price: Number (required),
discount: Number (default: 0),
image: Buffer (required),
bgColor: String (required),
textColor: String (required),
panelColor: String (required),
category: String (required)
}POST /users/register- Register new userPOST /users/login- User loginGET /users/logout- User logout
GET /owners/admin- Admin dashboardPOST /owners/create- Create owner (restricted to one)POST /owners/login- Owner loginGET /owners/logout- Owner logout
POST /products/create- Create new product (with image upload)
GET /- Home page with login/register modalsGET /shop- Product browsing page (protected)GET /cart- Shopping cart page (protected)GET /addtoCart/:productId- Add product to cart (protected)GET /removeFromCart/:productId- Remove product from cart (protected)
The application includes the following EJS templates:
index.ejs- Landing page with authentication modalsshop.ejs- Product catalog with search, filter, and sortcart.ejs- Shopping cart with quantity managementcreateProduct.ejs- Admin form for adding new productsabout.ejs- About page with company/app information
- Category Filtering: Filter products by category
- Sorting Options: Sort by name, price (low to high, high to low)
- Responsive Grid: Product cards with hover effects
- Add to Cart: One-click add to cart functionality
- Quantity Management: Increase/decrease product quantities
- Price Calculation: Real-time total calculation with discounts
- Remove Items: Remove products from cart
- Checkout Ready: Prepared for payment integration
- Modal-based Login/Register: Clean, user-friendly authentication
- Session Persistence: Maintain login state across browser sessions
- Password Security: bcrypt with salt rounds for password hashing
- JWT Tokens: Secure token-based authentication
- HTTP-Only Cookies: Prevent XSS attacks
- Input Validation: Mongoose schema validation
- Single Owner Policy: Prevents unauthorized admin creation
- Route Protection: Middleware-based route protection
- Node.js - Runtime environment
- Express.js - Web framework
- MongoDB - Database
- Mongoose - ODM for MongoDB
- JWT - Authentication tokens
- bcrypt - Password hashing
- Multer - File upload handling
- EJS - Template engine
- Tailwind CSS - Utility-first CSS framework
- Vanilla JavaScript - Frontend interactions
- Responsive Design - Mobile-first approach
- express-session - Session management
- connect-flash - Flash messaging
- cookie-parser - Cookie handling
- dotenv - Environment variable management
- Registration: Create account with personal details
- Browse Products: Use search, filter, and sort features
- Shopping Cart: Add products and manage quantities
- Account Management: Secure login/logout functionality
- Initial Setup: Create the first (and only) owner account
- Product Management: Add products with images and details
- Admin Dashboard: Access dedicated admin interface
- Complete user authentication system
- Shopping cart with persistent storage
- Product management with image upload
- Responsive design with modern UI
- Secure session management
- Uses
configpackage for environment-specific settings - Supports development and production configurations
- Environment variables for sensitive data
- Configured with Multer for product image uploads
- Images stored as Buffer in MongoDB
- Memory storage for efficient handling
- Wishlist: Already structured in user schema
- Order Management: Order array ready for implementation
- Payment Integration: Prepared cart system for payment APIs
- Product Reviews: Rate field in product schema
- Tailwind CSS for easy style modifications
- Responsive breakpoints already configured
- Dark mode ready structure
{
"bcrypt": "^6.0.0",
"config": "^4.0.0",
"connect-flash": "^0.1.1",
"cookie-parser": "^1.4.7",
"debug": "^4.4.1",
"dotenv": "^17.2.0",
"ejs": "^3.1.10",
"express": "^5.1.0",
"express-session": "^1.18.2",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.16.3",
"multer": "^2.0.1"
}- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the ISC License.
- Images stored in database (consider cloud storage for production)
- Basic error handling (can be enhanced)
- Order processing system (razorpay)
- Payment gateway integration
- Email notifications
- Advanced admin analytics
- Product review system
- Wishlist functionality
For support, please create an issue in the repository or contact the me (Email).
Built By Manab using Node.js, Express.js, EJS and MongoDB