A Node.js RESTful API for managing school locations and retrieving nearby schools based on geolocation coordinates. Built with Express.js and MySQL, this API is ideal for applications requiring spatial queries and distance-based sorting.
- 🚀 Add new schools with location data
- 📍 Retrieve schools sorted by proximity to a user's coordinates
- 🛢️ MySQL database integration
- 📐 Accurate geolocation-based distance calculations using the Haversine formula
Returns a list of schools sorted by distance from the provided coordinates.
Query Parameters:
latitude(required): Latitude of the user's locationlongitude(required): Longitude of the user's location
Example Response:
[
{
"name": "Green Valley High School",
"address": "123 Elm Street, Springfield",
"latitude": 25.2233,
"longitude": -67.3478,
"distance_km": 2.4
},
...
]git clone https://github.com/yourusername/school-management.git
cd school-managementnpm installCreate a .env file in the root directory and populate it:
PORT=8080
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=your_password
MYSQL_DATABASE=schoolmanagementRun the following command to set up the MySQL database schema:
mysql -u root -p < schema.sqlnpm run devnpm startBackend/
├── config/ # Database configuration
│ └── database.js
├── controllers/ # Request handlers
│ └── schoolController.js
├── models/ # Database queries and logic
│ └── schoolModel.js
├── routes/ # API routes
│ └── schoolRoutes.js
├── utils/ # Utility functions
│ └── geoUtils.js # Haversine distance calculator
├── app.js # Entry point
└── .env # Environment configuration
- Node.js / Express.js
- MySQL
- CORS for cross-origin resource sharing
- dotenv for environment variable management
Easily deploy this API to Render with:
This project is licensed under the MIT License.
Feel free to fork this repo, make enhancements, and open a pull request!