Building a Small Web API Project
Put Express, JSON, and an in-memory store together into one working mini API
- start an Express server and make it listen for requests,
- keep data in an in-memory array and send it back as JSON,
- build
GETandPOSTendpoints, read route params and the request body, - send correct status codes like
200,201, and404.
1. What "an API" really means
An API (Application Programming Interface) is a way for programs to talk to each other. A web API does this over HTTP: a client (a browser, a phone app, or a tool) sends a request, and your server sends back a response with some data.
Every request has a method (like GET to read or POST to create) and a path (like /books). Your job is to write a small function for each method+path pair. That function is called a route handler.
2. Starting the Express server
First we bring in Express, create an app, and tell it to listen on a port. The function you pass to app.listen runs once, when the server is ready.
const express = require('express');
const app = express();
const PORT = 3000;
app.listen(PORT, () => {
console.log('API running on http://localhost:' + PORT);
});
Run it with node server.js. The console prints:
API running on http://localhost:3000
The server now stays running, waiting for requests. It will not print anything else until a client connects.