🚀 Express.js
Beginner
What is the difference between app.use() and app.get() in Express?
Answer
app.use() registers middleware that runs for all HTTP methods matching the specified path (or all paths if no path is given). It is used for general-purpose middleware like body parsers, loggers, and authentication. If a path is specified, it matches any request whose URL starts with that path. app.get() registers a route handler for HTTP GET requests only and requires an exact path match (or parameterized match). Similarly, app.post(), app.put(), app.delete(), and app.patch() handle their respective HTTP methods. The key rule: app.use() for cross-cutting concerns, app.METHOD() for specific API endpoints.