Node.js REST API for Students
A simple REST API built with Node.js and Express to manage student records using JSON file storage. This project demonstrates CRUD operations (Create, Read, Update, Delete) and serves as a foundational example for building RESTful APIs.
Features
Create, read, update, and delete student records
Store data in a JSON file (students.json)
RESTful API design with standard HTTP methods
Easy to test with curl, Postman, or any HTTP client
Prerequisites
Node.js v14 or higher
npm (comes with Node.js)
Installation
Clone the repository:
git clone https://github.com/yourusername/rest-api-node.git cd rest-api-node
Install dependencies:
npm install
Create a data folder and an empty JSON file:
mkdir data echo "[]" > data/students.json
Start the server:
node server.js
The server will run at http://localhost:5000.
Usage
The API is accessible at http://localhost:5000/api/students . You can interact using curl, Postman, or any HTTP client.
API Endpoints Method Endpoint Description GET /api/students Get all students GET /api/students/:id Get a student by ID POST /api/students Create a new student PUT /api/students/:id Update a student by ID DELETE /api/students/:id Delete a student by ID
Examples
- Create a student curl -X POST -H "Content-Type: application/json" -d '{"name":"Alice"}' http://localhost:5000/api/students
Response:
{ "id": 1765131639140, "name": "Alice" }
- Get all students curl http://localhost:5000/api/students
Response:
[ {"id":1765131639140,"name":"Alice"} ]
- Get a student by ID curl http://localhost:5000/api/students/1765131639140
Response:
{ "id": 1765131639140, "name": "Alice" }
- Update a student curl -X PUT -H "Content-Type: application/json" -d '{"name":"Alice Updated"}' http://localhost:5000/api/students/1765131639140
Response:
{ "id": 1765131639140, "name": "Alice Updated" }
- Delete a student curl -X DELETE http://localhost:5000/api/students/1765131639140
Response:
{ "message": "Student deleted" }
Postman Collection
Screenshots
Get All Students
Create Student
Update Student
Delete Student