Serve JSON data
Expose a read-only resource endpoint.
Express.js · Function
Registers callback functions for HTTP GET requests matching a specified route path. It also handles HEAD requests for that path unless app.head() was registered there first.
Call app.get() with a path and one or more callback functions to register handling for matching HTTP GET requests. Express invokes the callbacks when a request matches both the route path and method; unless a separate HEAD handler was registered first for that path, this route also handles HEAD requests.
app.get(path, callback [, callback ...])| Name | Type | Description |
|---|---|---|
path | string or pattern accepted by Express routing | Route path to match, such as /health. |
callback | function | One or more callback functions invoked for a matching request. |
const express = require('express');
const app = express();
app.get('/health', (req, res) => {
res.json({ ok: true });
});
app.listen(3000);Creates a GET route that returns JSON at /health, then starts the server on port 3000.
Expose a read-only resource endpoint.
Return an HTML page for a browser route.
Provide a liveness or readiness endpoint.
app.get() registers a route for HTTP GET requests. Its first argument is the route path, followed by callback functions that Express runs when the path and method match.
Follow-up: How can several handlers participate in one route? Pass multiple callback functions; each handler must complete the response or delegate so processing can continue.
The method is attached to an Express application instance and uses a path plus callback functions. Express selects the route when an incoming request matches both the path and GET method.
Follow-up: How would you support another HTTP method? Use the corresponding application method, such as app.post(), app.put(), or app.delete().
app.get() also covers HTTP HEAD for its path when app.head() was not previously called for that path. This provides default HEAD handling without registering a separate handler.
Follow-up: How can the default behavior be overridden? Register app.head() for the path before registering app.get().
app.post()app.put()app.delete()app.head()app.route()express.Router