JSON APIs
In-memory collection
A beginner-friendly pattern: keep data in an array while learning routing.
const notes = [
{ id: 1, text: "Learn Express" },
];
app.get("/notes", (req, res) => {
res.json(notes);
});
Create with POST
Require JSON middleware, then push a new item.
app.post("/notes", (req, res) => {
const text = req.body?.text;
if (!text) {
return res.status(400).json({ error: "text is required" });
}
const note = { id: Date.now(), text };
notes.push(note);
res.status(201).json(note);
});
Try with curl
From another terminal while the server runs:
curl http://localhost:3000/notes
curl -X POST http://localhost:3000/notes \
-H "Content-Type: application/json" \
-d '{"text":"Ship it"}'
Later: a real database
Replace the array with SQLite, Postgres, or another store once routes feel comfortable.
Comments
One comment per signed-in account. Comments are saved with this page’s URL.