Every app needs an API. Express makes Node servers pleasant โ here is a complete CRUD API you can run right now.
Setup
mkdir api && cd api npm init -y npm install express
The whole API
const express = require("express");
const app = express();
app.use(express.json()); // parse JSON bodies
let students = [{ id: 1, name: "Priya", cgpa: 8.9 }];
app.get("/api/students", (req, res) => res.json(students));
app.get("/api/students/:id", (req, res) => {
const s = students.find(x => x.id === +req.params.id);
s ? res.json(s) : res.status(404).json({ error: "Not found" });
});
app.post("/api/students", (req, res) => {
const s = { id: Date.now(), ...req.body };
students.push(s);
res.status(201).json(s);
});
app.put("/api/students/:id", (req, res) => { /* find + update */ });
app.delete("/api/students/:id", (req, res) => {
students = students.filter(x => x.id !== +req.params.id);
res.status(204).end();
});
app.listen(3000, () => console.log("API on :3000"));REST conventions that interviews check
- GET reads, POST creates (201), PUT replaces, DELETE removes (204)
- Nouns in URLs (
/students), never verbs (/getStudents) - Status codes mean things: 400 bad input, 401 unauthenticated, 404 missing, 500 server bug
Test with Postman or curl, then deploy free on Render โ see the hosting guide.
โ All Articles