Entry Point File
Express Server Configuration Documentation
Overview
This script configures and starts an Express.js server for a web application, integrating various routes and functionalities.
Key Components:
-
Module Imports:
- Express, Cors, Dotenv, Mongoose, Body-parser for handling HTTP requests, cross-origin requests, environment variables, MongoDB integration, and request body parsing.
- Node-cron for scheduling tasks.
- Custom middleware and helpers.
-
Environment Setup:
dotenv.config(): Loads environment variables.
-
MongoDB Connection:
- Establishes a connection to MongoDB using Mongoose.
-
Express Middleware:
express.json(): Parses incoming requests with JSON payloads.bodyParser.urlencoded(): Parses incoming requests with URL-encoded payloads.cors(): Enables CORS.authenticateJWT: Custom middleware for JWT authentication.
-
Routes Configuration:
- Various routes (
/api/users,/api/auth, etc.) are configured using imported route modules. - A default route (”/”) sending a simple response.
- A catch-all route for 404 Not Found errors.
- Various routes (
-
Error Handling:
- Custom error handling middleware to catch and format errors.
-
Cron Job:
- Schedules
recurringPayment()function to run daily at midnight.
- Schedules
-
Server Initialization:
- The server listens on the specified port (5500).
Important Notes:
- The MongoDB URI is constructed using environment variables for security.
- The error handler middleware provides a consistent error response format.
- Ensure that the
.envfile is properly set up with required variables (MongoDB credentials, etc.). - The cron job for recurring payments is crucial for automatic processing.
Detailed Explanation of Express Server Middleware and Routes
-
JWT Authentication Middleware:
app.use(authenticateJWT): This middleware globally applies JWT authentication across all routes. It checks for a valid JWT in the request headers and sets the user’s information inres.localsfor further use in the request lifecycle.
-
API Routes:
- The app uses various specific routes for different functionalities:
/api/users: Handles user-related operations./api/auth: Manages authentication, including login and registration./api/payments: Deals with payment transactions and related data./api/checkout: Manages the checkout process and transaction completion./api/billingDetails: Manages user billing details./api/reports: Handles the generation and retrieval of reports.
- The app uses various specific routes for different functionalities:
-
404 Not Found Middleware:
- This middleware throws a
NotFoundErrorfor any requests to undefined routes, effectively catching all unmatched route requests.
- This middleware throws a
-
Error Handling Middleware:
- The last middleware in the chain handles errors. It formats and sends error responses with appropriate HTTP status codes. This ensures a consistent error response structure throughout the application.
Importance:
- The JWT middleware secures the application by ensuring only authenticated requests access specific routes.
- Each route handler is modular, focusing on a single aspect of the application (e.g., user management, payments), making the codebase organized and maintainable.
- The 404 and error handling middleware ensure that any invalid routes or errors in the application are handled gracefully, providing a better user experience.
Usage:
This script is the main entry point of the backend. It initializes the server, connects to the database, sets up routing, and handles errors. Ensure that all routes are correctly imported and middleware is applied as needed. The cron job should be monitored and adjusted according to business needs.