This is a Node.js-based School Management API that allows users to add schools with their respective locations and retrieve a sorted list of schools based on proximity to a given user location.
- Add new schools with name, address, latitude, and longitude.
- Fetch a list of schools sorted by distance from a user's location.
- Uses Haversine formula to calculate the distance between locations.
- Error handling for invalid or missing data.
- Deployed on Render with MySQL database hosted on Aiven.
- Node.js
- Express.js
- MySQL (Hosted on Aiven)
- Render (For API Deployment)
- EJS (for views)
- dotenv (for environment configuration)
-
Clone the repository:
git clone https://github.com/your-repo/school-management-api.git cd school-management-api -
Install dependencies:
npm install
-
Set up the environment variables: Create a
.envfile and add the following:PORT=3000 DB_HOST=your_aiven_db_host DB_USER=your_aiven_db_user DB_PASSWORD=your_aiven_db_password DB_NAME=your_aiven_db_name
-
Run the server:
npm start
Endpoint: POST /school
Request Body:
{
"name": "Test School",
"address": "Test Address",
"latitude": 0,
"longitude": 0
}Response:
{
"message": "School added successfully!",
"schoolId": 1
}Endpoint: GET /schools?latitude=0&longitude=0
Response:
{
"schools": [
{
"id": 1,
"name": "Test School",
"address": "Test Address",
"latitude": 0,
"longitude": 0,
"distance": 0
}
]
}.
├── models/
│ ├── school.js (Database model & queries)
├── routes/
│ ├── schoolRoutes.js (API routes)
├── views/
│ ├── index.ejs (Frontend views if needed)
├── public/ (Static files)
├── index.js (Main server file)
├── .env (Environment variables)
├── package.json (Dependencies & scripts)
└── README.md (Documentation)
- Returns
400 Bad Requestif required fields are missing or invalid. - Returns
500 Internal Server Errorfor database errors.
- Backend: Hosted on Render
- Database: MySQL hosted on Aiven
- Live API: Deployed Link
Udit Gupta