Routing and HTTP Methods
Objectives
By the end of this lesson, you should be able to:
- Define routes for
GET,POST,PUT, andDELETErequests - Explain how Express matches an incoming request to a route
- Understand what happens when no route matches
💡 Why this matters: REST APIs (covered fully in Module 9) are built entirely on matching an HTTP method and a URL path to a specific action, create, read, update, delete. Express’s routing is the mechanism that makes each of those a one-line registration instead of a manual check.
⚠️ A note on verification: every snippet and every response shown below was actually run and tested with real HTTP requests.
Routes for Different HTTP Methods
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Homepage');
});
app.get('/about', (req, res) => {
res.send('About page');
});
app.post('/contact', (req, res) => {
res.send('Contact form received');
});
app.put('/items/1', (req, res) => {
res.send('Item 1 updated');
});
app.delete('/items/1', (req, res) => {
res.send('Item 1 deleted');
});
app.listen(4002);
Testing each route with the matching method:
curl -X GET http://localhost:4002/
curl -X GET http://localhost:4002/about
curl -X POST http://localhost:4002/contact
curl -X PUT http://localhost:4002/items/1
curl -X DELETE http://localhost:4002/items/1
Homepage
About page
Contact form received
Item 1 updated
Item 1 deleted
app.get, app.post, app.put, and app.delete each register a handler for that specific HTTP method. A route only matches when both the path and the method match, GET /items/1 and PUT /items/1 are entirely separate routes, even though the path is identical.
What Happens With No Matching Route
curl -i -X POST http://localhost:4002/
HTTP/1.1 404 Not Found
Content-Type: text/html; charset=utf-8
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>Error</title></head>
<body><pre>Cannot POST /</pre></body>
</html>
Express automatically returns a 404 response when no registered route matches a request’s path and method, no code required for this default behavior. Module 8 (Error Handling) covers replacing this default page with a custom one.
Route Matching Order
Express checks routes in the order they’re registered, and uses the first one that matches. This matters once routes could overlap, a specific route like /users/me should typically be registered before a more general pattern like /users/:id (Lesson 4), otherwise the general pattern matches first and the specific route never gets reached.
Try It
- Create routes for
GET /products,POST /products, andDELETE /products, each responding with distinct plain text. - Test all three with
curl, using the correct method for each. - Send a request to a method/path combination you didn’t register, and confirm Express’s default 404 response appears.
- Explain, in your own words, why route registration order matters when two routes could both match the same path.
Recap
app.get/post/put/deleteregister a handler for a specific HTTP method and path, both must match.- Express returns a default 404 response automatically when nothing matches.
- Routes are checked in registration order, the first match wins.
Next lesson: the request and response objects every route handler receives.