Skip to content

Building a Small Web API Project

Programming Node.js Difficulty 1/5

Beginning of the lesson

Building a Small Web API Project

Put Express, JSON, and an in-memory store together into one working mini API

Big idea: A web API is a program that listens for requests and sends back data (usually JSON). In this topic you will build a tiny "books" API step by step, using everything you have learned: Express to handle requests, JSON as the data format, and an in-memory array as a simple store.
By the end you will be able to:
  • start an Express server and make it listen for requests,
  • keep data in an in-memory array and send it back as JSON,
  • build GET and POST endpoints, read route params and the request body,
  • send correct status codes like 200, 201, and 404.

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.

Client GET /books Server Express + data request response (JSON)
The client asks for data; the Express server reads its in-memory store and answers with JSON.

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.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers