Skip to content

About

A demo of daloyjs framework fully ready as a BFF server from generated daloyjs template in a monorepo setup.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

DaloyJS BFF Architecture Demo

A complete, runnable Backend-for-Frontend (BFF) showcase built on DaloyJS. One internal domain service is fronted by two purpose-built BFFs — one for a React web SPA, one for an Expo mobile app — each projecting view models tailored to its client.

The whole point: clients never talk to the domain service directly. They talk to a BFF that runs with DaloyJS' full browser-facing security posture, and the BFF talks to the domain service over an SSRF-guarded fetch. The domain service runs with preset: "internal-service" because it is never exposed to a browser.

┌──────────────┐     CORS + secureHeaders        ┌──────────────┐    fetchGuard()     ┌─────────────────────────┐
│  React SPA   │ ───────────────────────────────▶│   Web BFF    │ ───────────────────▶│                         │
│  (Vite 5173) │   GET /api/home, /api/books      │  (port 4001) │   GET /books …      │   Bookstore Service     │
└──────────────┘   rich web view models           └──────────────┘   SSRF-hardened     │   (port 4000)           │
                                                                                        │                         │
┌──────────────┐     CORS + secureHeaders        ┌──────────────┐    fetchGuard()     │  preset:                │
│  Expo app    │ ───────────────────────────────▶│  Mobile BFF  │ ───────────────────▶│  "internal-service"     │
│  (Metro 8081)│   GET /api/feed, /api/books      │  (port 4002) │   GET /books …      │  in-memory catalog      │
└──────────────┘   lean mobile view models        └──────────────┘   SSRF-hardened     └─────────────────────────┘

What this demonstrates

  • DaloyJS preset: "internal-service" on the domain service — keeps the 12 input/credential/SSRF guards (body limits, request timeouts, JWT allowlist, prototype-pollution-safe parsers, fetchGuard() defaults, response validation) while dropping the 4 topology guards (CORS/CSRF/secure-headers/proxy) that only matter when a browser is the direct caller.
  • Default browser-facing posture on both BFFs — requestId(), secureHeaders(), explicit cors() scoped to each client origin, and rateLimit(). Nothing is weakened.
  • fetchGuard() for every upstream hop — the BFFs call the domain service through an SSRF-hardened fetch, so a compromised or misconfigured upstream URL can't pivot to cloud metadata or arbitrary internal hosts.
  • Per-client view models — the same catalog is projected as rich cards for the web and lean cards for mobile, the classic reason to run a BFF per client.
  • Contract-first everything — every route is a Zod-typed DaloyJS contract with response validation on, and each service serves OpenAPI docs at /docs.

Layout

Package Role Port Stack
bookstore-api Internal domain service 4000 DaloyJS + internal-service preset
web-bff Browser-facing BFF for the SPA 4001 DaloyJS (default posture) + fetchGuard
mobile-bff Device-facing BFF for the Expo app 4002 DaloyJS (default posture) + fetchGuard
react-spa Web client 5173 Vite + React 19 + Tailwind v4 + shadcn/ui
ExpoMobile Mobile client 8081 Expo Router + React Native + NativeWind

Prerequisites

  • Node.js >= 24
  • pnpm >= 11

Install

pnpm install

Run everything

The three DaloyJS services and two frontends each run on their own port. Open five terminals, starting the domain service first so the BFFs have an upstream to call.

# 1 — internal domain service (start this first)
pnpm --filter bookstore-api dev      # http://localhost:4000

# 2 — web BFF
pnpm --filter web-bff dev            # http://localhost:4001

# 3 — mobile BFF
pnpm --filter mobile-bff dev         # http://localhost:4002

# 4 — React web SPA
pnpm --filter react-spa dev          # http://localhost:5173

# 5 — Expo mobile app
pnpm --filter expomobile start       # Metro on http://localhost:8081

Each service prints a startup banner with links to its docs (/docs), health (/healthz), and — for the domain service — its live security posture (/__security).

Tip: Copy the .env.example files in each package to .env if you need to change ports or upstream URLs. Defaults work out of the box for local dev.

One command, ordered boot

To start everything in dependency order from a single terminal:

pnpm dev:all

This runs scripts/dev-all.mjs, a dependency-free Node orchestrator that boots services in waves and waits for each one's health check before starting the next:

bookstore-api (4000)  →  web-bff (4001) + mobile-bff (4002)  →  react-spa (5173)

Logs from every service are prefixed and colorized, and Ctrl-C tears the whole stack down. (pnpm dev still works too, but Turborepo starts all five in parallel since persistent dev servers can't be sequenced.)

The Expo app is not included in pnpm dev:all — run it separately in its own terminal:

pnpm --filter expomobile start       # Metro on http://localhost:8081

Try the API directly

# Web BFF — rich aggregate for the SPA home screen
curl http://localhost:4001/api/home

# Web BFF — search/filter
curl "http://localhost:4001/api/books?category=scifi&q=dune"

# Mobile BFF — lean feed for the app
curl http://localhost:4002/api/feed

# Domain service security posture (internal-service preset in action)
curl http://localhost:4000/__security

Mobile networking notes

ExpoMobile/src/lib/api.ts resolves the BFF base URL automatically:

  • iOS simulator / web: http://localhost:4002
  • Android emulator: http://10.0.2.2:4002
  • Physical device: set EXPO_PUBLIC_MOBILE_BFF_URL=http://<your-LAN-ip>:4002 in ExpoMobile/.env (and add that origin to the mobile BFF's MOBILE_ORIGIN).

Tooling

  • Turborepo for monorepo task orchestration (pnpm build, pnpm typecheck, pnpm dev, pnpm lint).
  • pnpm with the repo's hardened install posture for package management.

Further reading

About

A demo of daloyjs framework fully ready as a BFF server from generated daloyjs template in a monorepo setup.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages