Server Overview
The backend is a lightweight Express.js server that acts as an intermediary between the React frontend and the Laravel backend service, while also executing the route-finding algorithm. Location:src/server/server.jsPort: 8080
Server Structure
- Static File Serving - Serves the React application
- API Endpoints - Handles route calculation requests
- Data Integration - Fetches bus stop data from Laravel backend
Static File Serving
/public directory are served at the root path, including:
index.html- Main HTML pagemain.js- React application bundlevendors.js- Third-party dependencies- CSS and other assets
API Endpoints
GET /allRoutes
Retrieves all available bus routes and stops from the backend. Location:src/server/server.js:17-24
This endpoint is currently unused by the frontend but available for future features like displaying all routes on the map.
POST /route
Calculates the optimal route between two geographic points. Location:src/server/server.js:27-38
Request Flow Diagram
ServerMockup Integration
The ServerMockup module abstracts the connection to the Laravel backend. Location:src/server/ServerMockup.js
Purpose
- Decouples backend logic from data source
- Makes it easy to swap in different data sources
- Provides a mockable interface for testing
Laravel Backend Contract
The Laravel backend is expected to expose: Endpoint:GET http://localhost:8000/branch
Expected Response:
Middleware Configuration
express.json() middleware is essential for:
- Parsing JSON request bodies
- Making
req.bodyavailable in route handlers - Required for the POST
/routeendpoint
Error Handling
Current error handling strategy:Errors are logged to console and returned to client. In production, consider:
- Using a proper logging service
- Sanitizing error messages for security
- Returning appropriate HTTP status codes
- Implementing error monitoring
Server Initialization
Architecture Patterns
Separation of Concerns
The backend demonstrates clear separation:| Component | Responsibility |
|---|---|
server.js | HTTP handling, routing, middleware |
ServerMockup.js | External data fetching |
FindLogic.js | Route calculation algorithm |
Stateless Design
The server maintains no session state:- Each request is independent
- No user authentication or sessions
- Easily horizontally scalable
Asynchronous Flow
All operations use Promises:Performance Considerations
Current Limitations
- No Caching: Bus stop data is fetched on every request
- Synchronous Algorithm: Route finding blocks the event loop
- No Request Validation: Input coordinates aren’t validated
Potential Optimizations
Implement Caching
Implement Caching
Cache bus stop data with periodic refresh:
Add Request Validation
Add Request Validation
Validate coordinates before processing:
Implement Rate Limiting
Implement Rate Limiting
Prevent abuse with rate limiting:
Security Considerations
Current security gaps:- No CORS configuration
- No input sanitization
- No authentication/authorization
- Error messages expose internal details
For production deployment, implement proper security measures including CORS, input validation, authentication, and sanitized error responses.
Next Steps
Learn more about:- Route Algorithm - How routes are calculated
- Frontend Architecture - Client-side implementation