Turuq Onboarding

Backend II

This section covers the essential backend technologies and architecture you'll be working with when using the Bun runtime and the Hono web framework.. Review these documents to understand our server-side infrastructure and development practices.

Overview

  • Bun is a fast JavaScript/TypeScript runtime and toolchain (runtime, package manager, bundler, test runner) focused on startup speed and developer performance. It aims to be a drop-in replacement for Node.js in many scenarios.
  • Hono is a lightweight, ultrafast web framework built on web standards that runs on many runtimes (Cloudflare Workers, Deno, Bun, Node.js, etc.). It offers a compact API and middleware system well suited for edge-style and server applications.

Why choose Bun + Hono?

  • Extremely fast cold-starts and short turnaround during local development.
  • Simple deployment targets and compatibility with edge/worker environments.
  • Small, focused framework (Hono) with first-class support for middleware and validation.

Routing & Middleware

  • Hono uses a small expressive API for routing (app.get, app.post, app.route) and supports nested routing via new Hono() sub-apps.
  • Middleware in Hono is lightweight; write functions that receive the context and next() and register them per-route or globally.
  • Hono ships helpers for common needs (CORS, static file serving, cookies, JWT helpers).

Validation

  • We use Zod for schema validation and type-safe parsing. Hono has examples and community middleware patterns for integrating Zod schemas for request bodies, params, and query strings.

Authentication & Authorization

  • Hono provides JWT helpers and middleware patterns for verifying tokens and protecting routes.
  • For sessions or cookie-based auth, as we do when using our third-party auth provider, Hono exposes cookie helpers and you can combine them with signed cookies or server-side session stores.

Recommended pattern: verify the token in a dedicated auth middleware and attach the user object to the Hono context (e.g. c.get('user') or c.set('user', user)), then use role checks in route handlers.

Error handling

  • Hono supports custom error handlers and middleware. Normalize errors into a consistent API error shape (status, code, message) and make sure to distinguish client vs server errors.

Testing

  • We use Bun’s test runner (bun test) or other test runners compatible with Bun. Hono handlers can be tested by invoking the app's request API or using supertest-like utilities.

Performance & deployment

  • Bun offers much faster startup and installs; use bun install for local development and CI to speed up dependency installs.
  • Hono is natively designed to run on edge runtimes and serverless platforms.

Tooling & developer experience

  • Use Bun for package management (bun add), running scripts (bun run), and testing (bun test).
  • Keep TypeScript strict enabled for safer code.

Security best practices

  • Validate and sanitize all inputs with Zod.
  • Use secure cookie flags, short token expiry, and refresh token rotation for long sessions.
  • Keep secrets in environment variables and use a secrets manager in production.

On this page