HTML and JSON Endpoints
In some cases, it’s useful to have multiple endpoints that return the same data, but in different formats, such as HTML versus JSON. Alternatively, a single endpoint can return data in multiple formats, choosing one based on the value of the accept request header.
The todo-hono[46] app implements both approaches. The data-fetching logic is shared by the two endpoints. This is a good approach when multiple endpoints can share complex data access logic like querying and updating databases.
First, we define the getAllTodos function which executes a SQL query to retrieve an array of Todo objects. This uses the SQLite support built into the Bun JavaScript engine, but the same approach applies to other database access libraries. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access