🎓 A1Learner
🔍
0 of 50 lessons completed

GET Routes

A GET route answers a read ask at one address. Express runs the matching route when the address is asked for.

A GET route

This route answers a read ask at /notes.

app.get("/notes", (req, res) => {
  res.json(notes);
});

Ask for the notes

This command asks the server for the notes.

curl http://localhost:3000/notes

Example

const express = require("express");

const app = express();
const notes = [{ id: 1, text: "Buy milk" }];

app.get("/notes", (req, res) => {
  res.json(notes);
});

app.listen(3000, () => {
  console.log("Notes API running on http://localhost:3000");
});

Key points

  • A GET route answers a read ask at one address.
  • Use app.get with a path and a function to make one.
  • The function takes a request and a response.
  • A request holds what the asker sent.
  • A response is what the server sends back.
  • Use res.json to send data back as JSON.

In this example, notes holds one note. The app.get line makes a GET route at /notes. The route function sends the notes back as JSON. The listen line starts the server on port 3000. A visit to /notes shows the note list.

Note

A GET route never changes stored data, so asking twice gives the same answer.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which line makes a GET route at /notes?