Skip to content

HTTP Server Connection Routes

Routes define how the HTTP server responds to incoming requests.

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" });
}
}

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" })
}

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.

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);
};
}
}

path - The URL path for WebSocket connections.

method - Must be “GET” for WebSocket upgrades.

handler - A callback that receives the request and can call request.accept() to upgrade to WebSocket.

HTTP Server Connection routes