This is a basic Express.js application with compression middleware to improve response time. The application also includes a main router for handling API routes.
-
Install the necessary dependencies:
npm install
-
Start the Express server:
npm start
The server will be running on http://localhost:3000 by default.
- Express: Fast, unopinionated, minimalist web framework for Node.js.
- Compression: Middleware for compressing responses.
- Main Router: The application includes a main router (
MainRouter.js
) for handling API routes. You can find and customize the routes in this file. - Middleware:
compression
: Used to compress responses, reducing the size of data sent to clients and improving performance.express.urlencoded
andexpress.json
: Middleware for parsing incoming request bodies.
- Default Route: The default route ("/") responds with a simple message: "Hello, My App!"
All API routes are prefixed with "/api". You can define and extend the routes in the MainRouter.js
file.
This repository contains a Node.js application that demonstrates real-time product updates using Socket.io and MongoDB. The application uses a WebSocket connection to notify clients about changes to product data in the MongoDB database.
Before running the application, make sure you have the following installed:
- Node.js
- MongoDB
- npm (Node Package Manager)
-
Clone the repository:
git clone https://github.com/your-username/real-time-product-updates.git cd real-time-product-updates
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory with the following content:PORT=3000 MONGODB_URI=your_mongodb_connection_string
Replace your_mongodb_connection_string
with the actual connection string for your MongoDB database.
This repository contains a set of routers built using Express.js for handling various endpoints of an application. Below is an overview of each router and its purpose:
-
Path:
/auth
Handles authentication-related endpoints.
-
Path:
/user
Handles user-related endpoints, requiring authentication.
-
Path:
/product
Manages endpoints related to product operations, requiring authentication.
-
Path:
/cartitem
Responsible for managing shopping cart items, requiring authentication.
-
Path:
/file
Manages file-related endpoints, requiring authentication.
-
Path:
/search
Handles search functionalities.
-
Location:
../validators/AuthValidator.js
Provides authentication checks for routes requiring authorization. Used as middleware for routes handling user, product, cart item, and file operations.
This router handles authentication-related endpoints.
-
Login
- Path:
/auth/login
- Method:
POST
- Description: Authenticates a user.
- Request Body:
- Requires validation using
UserValidator.loginValidator
. - Should include user credentials (e.g., email and password).
- Requires validation using
- Controller:
AuthController.loginController
- Path:
-
Register
- Path:
/auth/register
- Method:
POST
- Description: Registers a new user.
- Request Body:
- Requires validation using
UserValidator.registerValidator
. - Should include user details (e.g., username, email, password).
- Requires validation using
- Controller:
AuthController.registerController
- Path:
This router handles user-related endpoints.
-
Get User
- Path:
/user/:objectId
- Method:
GET
- Description: Retrieves a specific user by object ID.
- Request Parameters:
objectId
: The unique identifier of the user.
- Middleware:
UserValidator.getUserValidator
: Validates the request parameters.UserValidator.validate
: Additional validation if needed.
- Controller:
UserController.getUser
- Path:
-
Get All Users
- Path:
/user
- Method:
GET
- Description: Retrieves all users.
- Middleware:
UserValidator.getAllUsersValidator
: Validates the request.UserValidator.validate
: Additional validation if needed.
- Controller:
UserController.getAllUsers
- Path:
This router handles product-related endpoints.
-
Add Product
- Path:
/product/
- Method:
POST
- Description: Adds a new product.
- Request Body:
- Requires validation using
ProductValidator.addProductValidator
. - Should include product details.
- Requires validation using
- Controller:
ProductController.addProductController
- Path:
-
Edit Product
- Path:
/product/:objectId
- Method:
PUT
- Description: Updates an existing product.
- Request Parameters:
objectId
: The unique identifier of the product to be edited.
- Request Body:
- Requires validation using
ProductValidator.editProductValidator
. - Should include updated product details.
- Requires validation using
- Controller:
ProductController.editProductController
- Path:
-
Get All Products
- Path:
/product/
- Method:
GET
- Description: Retrieves all products.
- Controller:
ProductController.getAllProductController
- Path:
-
Get Product
- Path:
/product/:objectId
- Method:
GET
- Description: Retrieves a specific product by object ID.
- Request Parameters:
objectId
: The unique identifier of the product.
- Middleware:
ProductValidator.getProductValidator
: Validates the request parameters.
- Controller:
ProductController.getProductController
- Path:
-
Delete Product
- Path:
/product/:objectId
- Method:
DELETE
- Description: Deletes a specific product by object ID.
- Request Parameters:
objectId
: The unique identifier of the product to be deleted.
- Middleware:
ProductValidator.deleteProductValidator
: Validates the request parameters.
- Controller:
ProductController.deleteProductController
- Path:
This router handles cart item-related endpoints.
-
Add Item to Cart
- Path:
/cartitem/
- Method:
POST
- Description: Adds an item to the cart.
- Request Body:
- Requires validation using
CartItemValidator.addItemToCartValidator
. - Should include item details.
- Requires validation using
- Controller:
CartItemController.addItemToCart
- Path:
-
Get Cart Items
- Path:
/cartitem/
- Method:
GET
- Description: Retrieves items from the cart.
- Middleware:
CartItemValidator.getItemsValidator
: Validates the request.
- Controller:
CartItemController.getCartItems
- Path:
-
Get All Items
- Path:
/cartitem/all
- Method:
GET
- Description: Retrieves all items from the cart.
- Middleware:
CartItemValidator.getItemsValidator
: Validates the request.
- Controller:
CartItemController.getAllItems
- Path:
This router handles file-related endpoints.
-
Upload File
- Path:
/file/upload
- Method:
POST
- Description: Uploads a file.
- Request Body:
- Requires a single file upload using
multipart/form-data
format.
- Requires a single file upload using
- Controller:
FileController.uploadFile
- Path:
-
Get File
- Path:
/file/:filename
- Method:
GET
- Description: Retrieves a specific file by filename.
- Request Parameters:
filename
: The name of the file to be retrieved.
- Controller:
FileController.getFile
- Path:
-
Get All Files
- Path:
/file/
- Method:
GET
- Description: Retrieves metadata for all files.
- Controller:
FileController.getAllFiles
- Path:
This router handles search-related endpoints.
- Search
- Path:
/search/
- Method:
GET
- Description: Performs a search operation.
- Controller:
SearchController.searchController
- Path:
- Clone the repository.
- Install dependencies using
npm install
. - Start the server using
npm start
. - Access various endpoints using the provided paths and ensure authentication where necessary.
Authentication is required for routes handling sensitive operations such as user management, product management, cart operations, and file handling. The AuthValidator
middleware ensures that only authenticated users can access these routes.
-
Start the MongoDB server.
-
Run the application:
npm start
-
The server will be started at the specified port (default is 3000). Open your browser and navigate to http://localhost:3000 to access the application.
The application uses Socket.io to establish a WebSocket connection between the server and clients. When a client connects to the server, it joins a room specified by the product ID.
// Example: Joining a room based on the product ID
socketio.on("connection", (socket) => {
socket.join(socket.handshake.query.productId);
});
The application listens for changes in the MongoDB database using change streams. When a product is updated in the database, the server emits a product-update
event to the corresponding room, notifying clients about the change.
// Example: Broadcasting product updates to clients in the room
dbStream.on("change", (change) => {
if (change.operationType === "update") {
socketio.in(change.documentKey._id).emit("product-update", {
_id: change.documentKey._id,
...change.updateDescription.updatedFields,
});
}
});
Clients can then listen for the product-update
event and update their UI accordingly.
Feel free to contribute to the project by opening issues or submitting pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.