Q7: What is the difference between `app.use()`, `app.all()`, and `app.get()`?

Difficulty:EasyTopic:Routing
  • app.use(path, fn): Matches any HTTP method whose URL path begins with the target prefix. Used for mounting middleware.
  • app.all(path, fn): Matches all HTTP verbs for an exact match of the target route path.
  • app.get(path, fn): Matches only HTTP GET requests on an exact path match.
Routing Scope Differentiation
// Matches GET, POST, PUT on /api, /api/users, /api/v1/etc.
app.use('/api', authMiddleware);

// Matches GET, POST, DELETE specifically on /users/export
app.all('/users/export', exportHandler);

// Matches ONLY GET requests on /users
app.get('/users', getUsersHandler);

app.use acts as a path prefix match, app.all matches exact paths across verbs, and app.get filters by HTTP method.

Key Takeaways
  • Use app.use() for broad pipeline processing.

  • Use app.all() for applying route-level security logic across methods.