CT-store is an e-commerce backend system based on Node.js, Express, MongoDB, etc. This project provides a RESTful API for managing products, users, carts, and reviews. You can clone this repo for your e-commerce backend system.
app.js
: Main application fileroutes/
: Contains route files for different resourcesController/
: Contains controller files for handling requestsmodels/
: Contains model files for database schemasviews/
: Contains view templatesswagger.js
: Swagger setup for API documentation__test__/
: Contains test filesconfig/
: Contains .env, config setup
- Product Management: Create, read, update, and delete products.
- User Management: Manage user accounts and authentication.
- Cart Management: Add, update, and remove items from the cart.
- Review Management: Add and manage product reviews.
- API Documentation: Swagger-based API documentation.
- Error Handling: Comprehensive error handling for API requests.
- Testing: Unit, integration and stress tests for API endpoints.
- Node.js: JavaScript runtime for building server-side applications.
- Express: Web framework for Node.js.
- MongoDB: NoSQL database for storing application data.
- Mongoose: ODM (Object Data Modeling) library for MongoDB and Node.js.
- pm2: Simple implementation on cluster.
- Swagger: API documentation tool.
- Jest: Testing framework for JavaScript.
- Apache Jmeter: Load test on API.
- Supertest: Library for testing Node.js HTTP servers.
- Morgan: HTTP request logger middleware for Node.js.
- Cors: Middleware for enabling Cross-Origin Resource Sharing.
- Http-errors: Library for creating HTTP errors.
- Cookie-parser: Middleware for parsing cookies.
- Node.js (v12 or higher)
- MongoDB
Here provide two ways of setting up the environment:
-
Using Docker (recommended)
-
Clone the repository:
git clone https://github.com/your-username/CT-store.git cd CT-store
-
Setting up Docker image
docker compose up --build
After setting up all these, you can test api on port 3000 (or you can change it in docker-compose file).
-
-
Direct run on your local machine
-
Clone the repository:
git clone https://github.com/your-username/CT-store.git cd CT-store
-
Install dependencies:
npm install
-
Set up environment variables: Create a
.env
file in the config directory and add the following:```env JWT_SECRET = Set JWT SECRET KEY for jwt authorization JWT_EXPIRES_IN = 2h PORT=3000 DATABASE=Your mongodb database link ```
-
Start the application:
npm start npm start:prod # with pm2 load balancing
-
Access the API documentation: Open your browser and navigate to
http://localhost:3000/api-docs
to view the Swagger API documentation.
-
This section provides an alternative version of the application backend that includes a load balancing module. This module helps to increase the capability of handling a large number of requests efficiently.
-
Install PM2:
npm install pm2 -g
-
Configure the ecosystem.config.js file:
Create an
ecosystem.config.js
file in the root directory of your project and add the following content:module.exports = { apps: [ { name: 'app', script: './app.js', instances: 'max', exec_mode: 'cluster', autorestart: true, watch: true, max_memory_restart: '2G', env: { NODE_ENV: 'development', PORT: 3000, }, env_production: { NODE_ENV: 'production', }, }, ], };
-
Start the application:
pm2 start ecosystem.config.js
-
Check the application status:
pm2 status
-
Check system logs simultaneouly
pm2 logs
By following these steps, you can use PM2 to distribute your application across multiple CPU cores, achieving load balancing and improving the performance and stability of your application.
We welcome contributions to the CT Store project! If you have any ideas, suggestions, or bug reports, please feel free to open an issue or submit a pull request. Here are the steps to contribute:
-
Fork the repository:
Click the "Fork" button at the top right corner of this repository page.
-
Clone your forked repository:
git clone https://github.com/your-username/CT-store.git cd CT-store
-
Create a new branch:
git checkout -b feature/your-feature-name
-
Make your changes:
Implement your feature or fix the bug in your branch.
-
Commit your changes:
git add . git commit -m "Add your commit message here"
-
Push to your forked repository:
git push origin feature/your-feature-name
-
Create a pull request:
Go to the original repository and click the "New Pull Request" button. Provide a clear description of your changes and submit the pull request.
If you have any questions or need further assistance, please feel free to contact us:
- Email: [email protected]
Appreciate your feedback and support!