A simple RESTful API built with Deno, Hono.js, and PostgreSQL.
- Deno 1.32.0 or later
- PostgreSQL database
-
Clone the repository:
git clone https://github.com/yourusername/deno2-api.git cd deno2-api
-
Update the PostgreSQL connection string in
config/postgres.ts
. -
Run the server:
deno task start
The server will start on http://localhost:8000
by default.
config/
: Configuration files (e.g., database connection)routes/
: API route definitionshandlers/
: Request handlersservices/
: Business logic and database queriesutils/
: Utility functions and helpers
To create a new endpoint, follow these steps:
- Define your route in
routes/index.ts
. - Create a handler function in the
handlers/
directory. - Implement any necessary business logic and database queries in the
services/
directory.
Example of creating a new endpoint:
-
In
routes/index.ts
:import { getUserById } from "../handlers/userHandler.ts"; // ... existing code ... app.get("/users/:id", getUserById);
-
In
handlers/userHandler.ts
:import { Context } from "@hono/hono"; import { getUserService } from "../services/userService.ts"; export const getUserById = async (c: Context) => { const userId = c.req.param("id"); const user = await getUserService(userId); if (!user) { return c.json({ error: "User not found" }, 404); } return c.json(user); };
-
In
services/userService.ts
:import client from "../config/postgres.ts"; export const getUserService = async (userId: string) => { const result = await client.queryObject( "SELECT * FROM users WHERE id = $1", [userId], ); return result.rows[0] || null; };
This example demonstrates how to create a GET endpoint to retrieve a user by ID. It shows the separation of concerns between routing, handling requests, and interacting with the database.