REST Introduction
REST is a style for building web APIs. An API is a set of addresses a program can ask for data.
One read ask
This line prints a read ask for the notes list.
console.log("GET /notes");The answer as JSON
This code turns one note into a JSON string.
const notes = [{ id: 1, text: "Buy milk" }];
console.log(JSON.stringify(notes));Example
const routes = [
{ method: "GET", path: "/notes", action: "Read the notes" },
{ method: "GET", path: "/notes/1", action: "Read note 1" },
];
for (const route of routes) {
console.log(`${route.method} ${route.path} - ${route.action}`);
}Key points
- REST is a style for building web APIs.
- An API is a set of addresses a program can ask for data.
- Each address names one thing, like /notes for a list of notes.
- A method names the action.
- GET reads data.
- The server answers each ask with data, often in JSON.
- JSON is a text form for data that programs can read.
In this example, routes holds two addresses with their actions. Each entry names a method, a path, and an action. The loop prints one line for each route. The first line reads the whole list. The second line reads one note.
Note
REST is a style, not a library, so no install is needed for it.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does REST describe?