Serve GET resources
Expose a read-only endpoint such as /users or /health.
Express.js · Module
Registers callback functions for a route path and HTTP GET requests. The route also handles HEAD requests unless an earlier app.head() route exists for that path.
app.get() registers callback functions for a route path and the HTTP GET method. When an incoming GET request matches that path, Express runs the callbacks; unless a separate HEAD route was registered first, the same route also handles HEAD requests.
app.get(path, callback [, callback ...])| Name | Type | Description |
|---|---|---|
path | String or pattern | Route path to match against incoming requests. |
callback functions | Function | One or more callback functions invoked for matching requests. |
The documentation describes route registration and does not specify a return value for this use.
const express = require('express');
const app = express();
app.get('/hello', (req, res) => {
res.send('Hello, world!');
});
app.listen(3000);Creates a GET route at /hello and sends a plain-text response.
Expose a read-only endpoint such as /users or /health.
Return HTML, JSON, or text for a browser or API client.
Attach multiple handlers for validation, authentication, and response handling.
app.get() defines a route for HTTP GET requests. Its arguments are a path and one or more callback functions that Express invokes when the request matches.
Follow-up: How does it differ from app.use()? app.get() is method-specific, while app.use() mounts middleware without restricting it to GET.
The first argument is the route path, and the remaining arguments are callback functions. The callbacks handle the matching request and typically send a response or pass control onward.
Follow-up: What happens if no callback ends the request or calls next()? The request can remain unresolved until the client times out.
A registered app.get() route is also used for HEAD requests when app.head() was not registered for that path beforehand. This lets Express provide HEAD handling without duplicating the route.
Follow-up: How can HEAD behavior be customized? Register app.head() for the path before registering app.get().
app.head()app.post()app.put()app.delete()app.all()app.route()