HTTP Server Connection Routes
Routes define how the HTTP server responds to incoming requests.
Route Object
Section titled “Route Object”A route object defines how the HTTP server handles requests matching certain criteria.
{ path: "/api/data", method: "GET", handler: (request, response) => { response.send({ status: "ok" }); }}Static Data Route
Section titled “Static Data Route”A static data route serves fixed response data without invoking a handler.
{ path: "/health", method: "GET", status: 200, headers: { "Content-Type": "application/json" }, body: JSON.stringify({ status: "healthy" })}Properties
Section titled “Properties”
path- The URL path to match.
method- The HTTP method to match (GET, POST, PUT, DELETE, etc.).
handler(optional) - A callback function invoked when the route matches. If not provided, static data is returned.
status(optional) - The HTTP status code for static responses.
headers(optional) - An object of response headers.
body(optional) - The static response body.
WebSocket Handshake Route
Section titled “WebSocket Handshake Route”A WebSocket handshake route upgrades an HTTP connection to a WebSocket connection.
{ path: "/ws", method: "GET", handler: (request, response) => { // Upgrade to WebSocket const ws = request.accept(); ws.onmessage = (event) => { console.log(event.data); }; }}Properties
Section titled “Properties”
path- The URL path for WebSocket connections.
method- Must be “GET” for WebSocket upgrades.
handler- A callback that receives the request and can callrequest.accept()to upgrade to WebSocket.