API Routing Strategy
Relevant source files
The following files were used as context for generating this wiki page:
The API Routing Strategy in this project follows a modular Express-based architecture on the backend and a centralized Axios-based service layer on the frontend. The backend centralizes route declarations in a primary application file, which delegates specific endpoint logic to specialized router modules. This ensures a clean separation of concerns between user management, AI-driven chatbot interactions, external news aggregation, and product discovery.
On the frontend, the routing strategy involves a mapping of React components to URL paths, complemented by a service layer that abstracts the HTTP communication with the backend. This multi-layered approach facilitates secure communication through JWT verification and standardized API response formats.
Backend Route Architecture
The backend entry point BACKEND/src/app.js serves as the central hub for mounting various middleware and route modules. It uses a versioned prefix /api/v1 for all routes to support future API iterations.
Centralized Route Declaration
The application uses the app.use() method to attach specific router modules to their respective base paths. This allows for clean organizational boundaries between different functional domains.
The diagram shows how the main application delegates routing to specialized sub-routers.
Sources: BACKEND/src/app.js:25-34
Route Modules and Implementation Details
| Route Prefix | Source File | Key Responsibilities |
|---|---|---|
/api/v1/users | user.routes.js | User registration, authentication, token refreshing, and password management. |
/api/v1/chatbot | chatbot.routes.js | Processing AI queries and managing networking assistant interactions. |
/api/v1/articles | articles.routes.js | Fetching entrepreneurship news and generating AI-based summaries. |
/api/v1/products | producthunt.routes.js | Aggregating top products from the Product Hunt GraphQL API. |
Sources: BACKEND/src/app.js:27-33, BACKEND/src/routes/user.routes.js:21-31, BACKEND/src/routes/articles.routes.js:41-43
Middleware and Security Integration
The routing strategy incorporates several security layers directly into the route definitions. This includes rate limiting to prevent brute-force attacks and JWT verification for protected resources.
Request Flow with Middleware
When a request hits a protected route, it passes through a series of checks before reaching the controller. For example, the logout route uses both a rate limiter and a JWT verification middleware.
The sequence diagram illustrates the tiered security approach applied to sensitive user routes.
Sources: BACKEND/src/routes/user.routes.js:10-27, BACKEND/src/app.js:15-22
Rate Limiting Configuration
Specific routes are protected by different rate limiting instances to balance security and usability:
- Register Rate Limiter: Limits account creation attempts to 100 requests per 15 minutes.
- Logout Rate Limiter: Limits logout attempts to 100 requests per 15 minutes.
Sources: BACKEND/src/routes/user.routes.js:10-20
Frontend Service Layer
The frontend implements a mirrored routing strategy through a service abstraction layer. This layer ensures that all outgoing requests are formatted correctly and include necessary authentication headers.
Centralized API Client
The frontend/front/src/services/api.js file configures a global Axios instance with a base URL of http://localhost:8000/api/v1. It employs a request interceptor to automatically inject the Bearer token from local storage into the Authorization header.
api.interceptors.request.use((config) => {
const token = localStorage.getItem("token");
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
Sources: frontend/front/src/services/api.js:14-20
Component-to-API Mapping
React components utilize specific service functions to interact with backend endpoints:
- LoginPage/SignupPage: Uses
login()andsignup()services mapping to/users/loginand/users/register. - ArticlesPage: Directly calls
/api/v1/articlesusing aqueryparameter (defaulting to "entrepreneurship"). - ProductsPage: Fetches from
/api/v1/products.
Sources: frontend/front/src/services/api.js:23-45, frontend/front/src/Article/article.jsx:79-81, frontend/front/src/Products/Productspage.jsx:92-94
Client-Side Navigation
The frontend utilizes react-router-dom to define the mapping between browser URLs and UI views. This routing strategy ensures that the application behaves as a Single Page Application (SPA).
| URL Path | Component | Description |
|---|---|---|
/ | LoginPage | Default entry point for authentication. |
/dashboard | Dashboard | Landing page after login with carousel features. |
/chatbotpage | ChatbotPage | The primary AI networking assistant interface. |
/articles | ArticlesPage | Interface for viewing AI-summarized industry news. |
/products | ProductsPage | Showcase for trending products. |
/help | Help | FAQ and contact information section. |
Sources: frontend/front/src/App.jsx:18-35
Conclusion
The API routing strategy of the EICHackathon project is characterized by a strong modular structure. By separating route definitions into domain-specific files and centralizing frontend API calls into a dedicated service layer, the architecture maintains high maintainability. The integration of security middleware like rate limiting and JWT verification directly into the routing pipeline ensures that sensitive user data and AI resources are protected while providing a seamless navigation experience across the frontend SPA.