A RESTful API for a multi-seller marketplace built with Node.js, Express, and MongoDB. This API enables multiple sellers to list products and allows buyers to place orders in a centralized marketplace system.
Repository: https://github.com/KnockoutCoder/multi-seller-marketplace.git
- User Management: Create and manage users with roles (buyer, seller, admin)
- Product Management: Sellers can create, update, and manage their product listings
- Order Management: Buyers can place orders with multiple products
- Soft Deletion: Products use soft deletion for data integrity
- Swagger Documentation: Interactive API documentation
- Comprehensive Testing: Unit tests with Jest and coverage reporting
- Security: Helmet.js for security headers, CORS configuration
- Error Handling: Centralized error handling middleware
- Runtime: Node.js (ES Modules)
- Framework: Express.js 5.1.0
- Database: MongoDB with Mongoose
- Documentation: Swagger/OpenAPI 3.0
- Testing: Jest with MongoDB Memory Server
- Security: Helmet, CORS
- Development: Nodemon for hot reloading
Before you begin, ensure you have the following installed:
- Node.js (v18 or higher recommended)
- npm (comes with Node.js)
- MongoDB (local instance or MongoDB Atlas account)
-
Clone the repository:
git clone https://github.com/KnockoutCoder/multi-seller-marketplace.git cd multi-seller-marketplace -
Install dependencies:
npm install
This will install all dependencies and development dependencies listed in
package.json.Key dependencies included:
express- Web framework for Node.jsmongoose- ODM (Object Document Mapper) for MongoDBdotenv- Loads environment variables from .env filecors- Enables Cross-Origin Resource Sharinghelmet- Security middleware for Expressswagger-jsdoc&swagger-ui-express- API documentation
Development dependencies included:
nodemon- Automatically restarts the server when files change (development only)jest- Testing frameworkmongodb-memory-server- In-memory MongoDB for testingbabel-jest&@babel/core- JavaScript transpilation for testing
-
Set up environment variables: Create a
.envfile in the root directory:MONGODB_URI=your_mongodb_connection_string PORT=3000
For local MongoDB:
MONGODB_URI=mongodb://localhost:27017/kc-mart PORT=3000
-
Start the development server:
npm run dev
Or start the production server:
npm start
The server will start on http://localhost:3000 (or the port specified in your .env file).
npm start- Start the production servernpm run dev- Start the development server with nodemon (auto-reload)npm test- Run all testsnpm run test:watch- Run tests in watch modenpm run test:coverage- Run tests with coverage report
The project uses Jest for testing with MongoDB Memory Server for isolated test databases.
Run all tests:
npm testRun tests with coverage:
npm run test:coverageView coverage reports in the coverage/ directory. The project maintains a 60% coverage threshold for branches, functions, lines, and statements.
- GitHub Repository: https://github.com/KnockoutCoder/multi-seller-marketplace.git
- Live API Documentation (Swagger): https://kc-mart-api.onrender.com/api-docs/
- Frontend Demo (Storefront & Seller Dashboard): https://kc-mart.vercel.app/
- Production API: https://kc-mart-api.onrender.com
The Swagger UI provides an interactive interface to explore and test all API endpoints. Visit the live API documentation to try out the API endpoints directly from your browser.
You can test the API using the provided frontend demo which includes a storefront for buyers and a seller/admin dashboard at https://kc-mart.vercel.app/.
- Production:
https://kc-mart-api.onrender.com - Local Development:
http://localhost:3000
POST /users- Create a new userGET /users- Get all usersGET /users/:id- Get a user by IDPATCH /users/:id- Update a user (partial update)
User Roles: buyer, seller, admin
POST /products- Create a new product (requires seller role)GET /products- Get all active products (supportscategoryandsellerIdquery params)GET /products/:id- Get a product by IDPATCH /products/:id- Update a product (partial update)DELETE /products/:id- Soft delete a product (setsisActiveto false)
POST /orders- Create a new orderGET /orders- Get all orders (supportsbuyerIdquery param)GET /orders/:id- Get an order by ID
Order Status: pending, paid, cancelled
src/
βββ app.js # Express app configuration
βββ server.js # Server entry point
βββ config/
β βββ db.js # MongoDB connection
β βββ swagger.js # Swagger configuration
βββ controllers/ # Request handlers
β βββ user.controller.js
β βββ product.controller.js
β βββ order.controller.js
βββ models/ # Mongoose schemas
β βββ User.js
β βββ Product.js
β βββ Order.js
βββ routes/ # API routes
β βββ user.routes.js
β βββ product.routes.js
β βββ order.routes.js
βββ services/ # Business logic layer
β βββ user.service.js
β βββ product.service.js
β βββ order.service.js
βββ middleware/ # Custom middleware
βββ errorHandler.js
βββ notFound.js
name(required)email(optional, unique)role(required:buyer,seller,admin)createdAt,updatedAt(auto-managed)
title(required)description(optional)price(required, min: 0)stock(required, min: 0)category(required)image(optional: URL or base64)sellerId(required, references User)isActive(default: true, for soft deletion)createdAt,updatedAt(auto-managed)
buyerId(required, references User)status(default:pending, enum:pending,paid,cancelled)totalAmount(required, min: 0)items(required array of order items)productId(references Product)quantity(min: 1)unitPricesubtotal
createdAt,updatedAt(auto-managed)
- Helmet.js: Sets various HTTP headers for security
- CORS: Configurable Cross-Origin Resource Sharing
- Input Validation: Mongoose schema validation
- Error Handling: Centralized error handling to prevent information leakage
- The API supports base64 image uploads (50MB limit for JSON payloads)
- Products use soft deletion (setting
isActive: false) rather than hard deletion - Order items store the price at the time of order (
unitPrice) to preserve order history - Email field in User model is optional and allows multiple null values (sparse unique index)
KC Ninal
- Email: homebasedkc@gmail.com
This project is licensed under the MIT License.
This is a decoupled backend API. Feel free to use it as a reference or integrate it with your own frontend application.
Happy Coding! π