Skip to content

Basic Routing

In Express, Routing is our GPS. It determines how our application responds to a client request for a particular endpoint.

The structure is simple: app.METHOD(PATH, HANDLER).

  • app: The instance of express.
  • METHOD: The HTTP request method (get, post, put, delete, etc.).
  • PATH: A path on the server.
  • HANDLER: The function executed when the route is matched.
A complex, neon-glowing highway interchange from a bird's eye view. Different colored lanes represent GET (Blue), POST (Green), PUT (Yellow), and DELETE (Red).

Fig 1: The Routing Logic. Follow the colors or get lost in traffic.


// The "GET" Lane -> Retrieve Info
// Path: Root ("/")
app.get('/', (req, res) => {
res.send('Engine Online. Hello Express!');
});
// Path: /things
app.get('/things', (req, res) => {
res.send('Here is the list of things you asked for.');
});

Solo Warning: Express matches routes in the order they are defined. First match wins. If you have two routes for /things, the second one will never be reached. Traffic doesn’t loop back.


Basic Express Routing - Demo Repo

⏭ Designated Lanes

We can handle generic traffic. Now let’s talk about specific targets using named route parameters.