Skip to content

Backend Architecture

The backend is a Node.js REST API built with Hapi.js 21.x.

Project Structure

backend/
├── authorization/      # Auth strategies and policies
├── config/             # Database, ORM, and queue configuration
├── constants/          # Application-wide constants
├── constructors/       # Object constructors / factories
├── controllers/        # Route handlers
├── emails/             # Email templates (MJML / Twig)
├── jobs/               # Background job definitions (Bull)
├── libraries/          # Shared utility libraries
├── middlewares/         # Hapi request lifecycle hooks
├── models/             # Database models
├── routes/             # API route definitions
├── schemas/            # Joi validation schemas
├── services/           # Business logic layer
├── tests/              # Mocha / Jest test suites
├── server.js           # HTTP server entry point
└── worker.js           # Background worker entry point

Key Patterns

Request Lifecycle

Route → Middleware → Controller → Service → Model → Database

Authentication

The API supports multiple auth strategies:

  • JWT (hapi-auth-jwt2) — Primary token-based auth
  • Bearer tokens (hapi-auth-bearer-token)
  • Basic auth (@hapi/basic)
  • 2FA via speakeasy (TOTP)

Validation

All request payloads are validated using Joi schemas defined in the schemas/ directory.

Background Jobs

Asynchronous tasks are processed through Bull queues backed by Redis. Job definitions live in the jobs/ directory.

Email

Emails are rendered using MJML templates (with Twig for dynamic content) and sent via Mailgun.

INFO

For detailed API conventions and endpoint patterns, see API Conventions.

Envia Admin