Skip to content

code-kasha/school_api

Repository files navigation

School Management API

A RESTful API built with Node.js, Express.js, and MySQL for managing school data.

The API allows users to:

  • Add new schools
  • Retrieve schools sorted by proximity to a user's location

Deployment Notes

Free Tier Hosting Notice

This project is deployed using:

  • Render (Backend Hosting)
  • Railway (MySQL Database)

Both services are running on their free tiers.

Because of this:

  • The API may take 30–60 seconds to respond on the first request after inactivity.
  • Railway databases may occasionally sleep when unused for extended periods.
  • Temporary delays or cold starts are expected on the first API call.

If the API appears unresponsive initially, please wait briefly and retry the request.


Live API

https://school-api-hfj5.onrender.com


Health Check

You can verify the API is active using:

GET /

Expected response:

{
  "success": true,
  "message": "School Management API is running"
}

Features

  • Node.js + Express.js
  • MySQL Database
  • ES Modules
  • Input Validation
  • Distance Calculation using Haversine Formula
  • Sorted Nearby Schools
  • Clean Folder Structure

Tech Stack

  • Node.js
  • Express.js
  • MySQL
  • mysql2
  • express-validator
  • dotenv
  • cors
  • nodemon

Project Structure

school-management-api/
│
├── config/
│   └── db.js
│
├── controllers/
│   └── schoolController.js
│
├── routes/
│   └── schoolRoutes.js
│
├── .env
├── .gitignore
├── package.json
├── README.md
├── server.js
└── schema.sql

API Endpoints

Note: The application is hosted on Render and Railway free tiers, so the first request may take some time due to cold starts.


1. Add School

Endpoint

POST https://school-api-hfj5.onrender.com/addSchool

Request Body

{
  "name": "Green Valley School",
  "address": "Mumbai",
  "latitude": 19.0760,
  "longitude": 72.8777
}

Success Response

{
  "success": true,
  "message": "School added successfully"
}

Validation Rules

  • name → required
  • address → required
  • latitude → valid float between -90 and 90
  • longitude → valid float between -180 and 180

2. List Schools

Endpoint

GET https://school-api-hfj5.onrender.com/listSchools?latitude=19.0760&longitude=72.8777

Success Response

{
  "success": true,
  "count": 2,
  "data": [
    {
      "id": 1,
      "name": "Green Valley School",
      "address": "Mumbai",
      "latitude": 19.076,
      "longitude": 72.8777,
      "distance_km": 0
    },
    {
      "id": 2,
      "name": "Sunrise Public School",
      "address": "Pune",
      "latitude": 18.5204,
      "longitude": 73.8567,
      "distance_km": 120.52
    }
  ]
}

3. Test DB

Endpoint

GET https://school-api-hfj5.onrender.com/test-db

Success Response

{
    "success": true,
    "rows": [
        {
            "1": 1
        }
    ]
}

Distance Calculation

The API uses the Haversine Formula to calculate the geographical distance between the user and each school.

This ensures accurate earth-surface distance calculations.


Example Request Using Fetch

const response = await fetch(
  "https://school-api-hfj5.onrender.com/listSchools?latitude=19.0760&longitude=72.8777"
);

const data = await response.json();

console.log(data);

Author

Akash Damle

About

A RESTful API built with Node.js, Express.js, and MySQL for managing school data.

Topics

Resources

Stars

Watchers

Forks

Contributors