How gargdivyansh1/educaseindia_backendassignment works
Project Type and Purpose
• This is a backend REST API service (Node.js + Express) for managing “School” records in a MySQL database.
• Primary functions: add a school and list schools sorted by proximity to a given coordinate.
File Structure Analysis
• index.js: application entry point; boots Express, applies middleware, mounts routes.
• config/db.js: database connection configuration using MySQL.
• routes/schoolRoutes.js: maps HTTP endpoints to controller methods.
• controllers/schoolController.js: contains request handlers, calls validation, business logic, and DB layer.
• validations/schoolValidation.js: request‐payload schema checks.
• utils/distance.js: contains the algorithm for calculating geospatial distance.
• sql/create_school_db.sql: DDL for setting up the MySQL “schools” table.
Key Technologies and Patterns
• Express.js for HTTP routing and middleware pipeline.
• MVC‐style layering (Routes → Controllers → Data Layer).
• MySQL relational database accessed via a DB client in config/db.js.
• Utility module for shared logic (distance calculation).
• Hosted on Railway (external PaaS).
Main Components
a. Client (e.g., Postman or frontend) sending HTTP requests.
b. Express API Server:
• HTTP Listener (index.js)
• Router Layer (schoolRoutes.js)
• Controller Layer (schoolController.js)
• Validation Layer (schoolValidation.js)
• Utility Layer (distance.js)
c. MySQL Database (configured via config/db.js; schema defined in sql/create_school_db.sql).
d. External Hosting Platform (Railway).
Interactions and Data Flow
1. Client issues HTTP request to /api/addSchool or /api/listSchools.
2. Express Listener forwards to appropriate Route.
3. Route invokes Validation middleware.
4. If valid, Controller executes:
• For POST: inserts new record into MySQL.
• For GET: queries all schools, passes results to distance util, sorts by computed distance.
5. Controller sends JSON response back to Client.
6. MySQL serves as persistent store; connection pooled in config/db.js.
Architectural Patterns and Principles
• Layered architecture: separate concerns (routing, validation, business logic, data access).
• Single responsibility per module.
• Reusable utility functions for cross‐cutting logic (distance).
• Externalize configuration (DB credentials, host) from code.
Detailed Checklist
• [ ] All modules labeled clearly (Routes, Controllers, Utils, Validation).
• [ ] Data flows annotated with HTTP methods and SQL operations.
• [ ] Connection pool or DB config represented in config/db.js.
• [ ] Utility function invocation for distance.
• [ ] Hosting environment boundary indicated.