Express app.METHOD() Functions
What you’ll learn
- How route methods like
app.get(),app.post(), andapp.delete()work. - How to map CRUD operations to proper HTTP methods.
- How to apply middleware chains and organize route handlers.
- How to avoid route-order and method-mismatch issues.
Overview
Express route methods follow HTTP semantics: each method handler runs only for its matching HTTP verb.
Method-specific routes
app.get() handles GET, app.post() handles POST, and so on.
REST alignment
Use method intent clearly: GET(read), POST(create), PUT/PATCH(update), DELETE(remove).
Predictable behavior
Method-scoped routes reduce accidental matching and improve API clarity.
Syntax
javascript
app.METHOD(path, callback)
app.METHOD(path, middleware1, middleware2, ..., callback)- METHOD: lowercase HTTP verb like
get,post,put,patch,delete. - path: route pattern to match.
- callback chain: middleware + final handler.
1
CRUD style route methods
javascript
app.get('/users', function (req, res) {
res.send('List users');
});
app.post('/users', function (req, res) {
res.send('Create user');
});
app.put('/users/:id', function (req, res) {
res.send('Replace user');
});
app.delete('/users/:id', function (req, res) {
res.send('Delete user');
});2
Method route with middleware chain
javascript
function auth(req, res, next) {
if (!req.user) return res.status(401).send('Unauthorized');
next();
}
app.get('/account', auth, function (req, res) {
res.json({ id: req.user.id, name: req.user.name });
});📋 app.METHOD() vs app.all()
| Approach | Scope | Best use |
|---|---|---|
app.METHOD() | Single HTTP method | Precise endpoint behavior |
app.all() | All HTTP methods | Shared route-level checks |
🧪 Testing checklist
- Call the same path with different HTTP methods and verify method-specific responses.
- Confirm middleware chain executes in declared order.
- Check status codes for unsupported methods and invalid inputs.
- Validate route params and body parsing for update/create methods.
Pitfalls to avoid
Wrong method choice
Semantic confusion
Use verbs aligned with operation intent to keep APIs predictable.
Route order conflicts
Unexpected matching
Place specific method routes before broad wildcard patterns.
Missing middleware exit
Hanging requests
Ensure each middleware either sends a response or calls next().
❓ FAQ
It is a route function pattern where METHOD is an HTTP verb, such as get, post, put, patch, or delete.
Different HTTP methods represent different operations (read, create, update, delete) for the same resource path.
Yes. You can pass one or more middleware functions before the final handler.
app.METHOD() handles one specific verb, while app.all() runs for all HTTP methods on the matching path.
Express returns its default 404 response unless you provide custom fallback handlers.
Summary
- Core idea:
app.METHOD()binds one handler flow to one HTTP verb. - Design: map routes to REST-friendly method semantics.
- Reliability: organize route order and middleware exits carefully.
Did you know?
Express provides route methods like app.get(), app.post(), and app.delete(); each maps to a specific HTTP verb.
4 people found this page helpful
