/Fastify-Starter

:leopard: A boilerplate for Node.js, Fastify, TypeScript, Vite, Vitest, and Render. I18n | MongoDB | JWT | OTP | Redis | Cloudinary | WebSocket | EventSource | Nodemailer | BullMQ | ESLint | Prettier | Playwright | SuperTest | AutoCannon | Pnpm | PM2 | Caddy | Docker | CircleCI | TypeBox

Primary LanguageTypeScriptMIT LicenseMIT

Fastify Starter

🐆 A boilerplate for Node.js, Fastify, TypeScript, Vite, Playwright, and Render.

🌈 Live Demo - The client application is here.

Table of Contents

Getting Started

Get started with Fastify Starter.

# dev server
$ pnpm install
$ pnpm dev

# mock server
$ cd mock/requests && pnpm install && cd ../..
$ pnpm mock

Project Setup

Follow steps to execute this boilerplate.

Install dependencies

$ pnpm install

Compiles and hot-reloads for development

$ pnpm dev

Compiles and minifies for production

$ pnpm build

Locally preview the production build

# Before running the `preview` command, make sure to run the following commands.
$ pnpm build

$ pnpm preview

Lints and fixes files

Files: src/**/*.ts

$ pnpm lint

Runs unit tests

Files: src/**/*.spec.ts

$ pnpm unit

Runs end-to-end tests

Files: e2e/**/*.spec.ts

# Before running the `e2e` command, make sure to run the following commands.
$ pnpm build
$ pnpm preview

# If it's not installed, run it.
$ cd e2e && pnpm install && cd ..

$ pnpm e2e

Performance metrics

Files: benchmark/**/*.spec.ts

# Before running the `bench` command, make sure to run the following commands.
$ pnpm build
$ pnpm preview

# If it's not installed, run it.
$ cd benchmark && pnpm install && cd ..

$ pnpm bench

Mock requests

# If it's not installed, run it.
$ cd mock/requests && pnpm install && cd ../..

$ pnpm mock

Documentation

Files: docs/**/*.md

# If it's not installed, run it.
$ cd docs && pnpm install && cd ..

$ pnpm doc

Key Features

This seed repository provides the following features:

  • ---------- Essentials ----------
  • Fastify - Web Application Framework
  • Routes - File-based Routing
  • MongoDB - Document Database
  • JWT - Authentication
  • PBKDF2 - Hash Passwords
  • OTP - Authenticator
  • Cloudinary - Asset Management
  • I18n - Internationalization and Localization
  • Redis - In-memory Data Structure Store
  • WebSocket - Two-way Interactive Communication Session
  • EventSource - Server-sent Events
  • Mailer - Email Sending
  • Email - Email Builder
  • Nunjucks - Email Rendering
  • Sheets - Spreadsheet Data Toolkit
  • BullMQ - Message Queue
  • ---------- Tools ----------
  • Vite - Bundler
  • TypeScript - JavaScript with Syntax for Types
  • ESLint - Linter
  • Prettier - Formatter
  • Vitest - Test Runner
  • Playwright - Test Automation
  • Autocannon - Measure Performance
  • VitePress - Documentation
  • ---------- Environments ----------
  • Node.js - JavaScript Runtime Environment
  • Pnpm - Package Manager
  • PM2 - Process Manager
  • Caddy - Web Server
  • Docker - Containerized Application Development
  • CircleCI - Continuous Integration and Delivery
  • Render - Cloud Application Hosting

Tiny examples

Configuration

Control the environment.

Default environments

Set your local environment variables.

// env.ts
export default {
  NODE_ENV: process.env.NODE_ENV || 'development',

  HOST: process.env.HOST || '127.0.0.1',
  PORT: process.env.PORT || 3000,

  SITE_URL: process.env.SITE_URL || 'http://127.0.0.1:5173',

  MONGODB_URL: process.env.MONGODB_URL || 'xxx',
  REDIS_URL: process.env.REDIS_URL || 'xxx',
  CLOUDINARY_URL: process.env.CLOUDINARY_URL || 'xxx',
  SMTP_URL: process.env.SMTP_URL || 'xxx',

  SECRET_KEY: process.env.SECRET_KEY || 'xxx',
};

Continuous integration environments

Add environment variables to the CircleCI build.

# production
DEPLOY_HOOK=xxx

# development
DEV_DEPLOY_HOOK=xxx

# staging
STAGE_DEPLOY_HOOK=xxx

Continuous delivery environments

Add environment variables to the Render build.

SITE_URL=xxx

MONGODB_URL=xxx
REDIS_URL=xxx
CLOUDINARY_URL=xxx
SMTP_URL=xxx

SECRET_KEY=xxx

Directory Structure

The structure follows the LIFT Guidelines.

.
├── .circleci
├── benchmark -> load testing
├── docs -> api documentation
├── e2e -> e2e testing (Caddy Server proxy api and proxy mock api)
├── mock
│   ├── requests -> mock third-party api
│   └── responses -> mock data for mock api, unit testing, and e2e testing
├── public -> not handled by vite, copy it to dist
├── src
│   ├── assets -> wasm
│   ├── components -> shared module
│   ├── composables -> shared module
│   ├── locales -> core module
│   ├── middleware -> core module
│   ├── plugins -> root module
│   ├── routes -> feature modules
│   ├── templates -> email templates with nunjucks
│   ├── utilities -> shared module
│   ├── app.ts
│   ├── error.ts
│   ├── main.ts
│   └── shims.d.ts
├── .editorconfig
├── .eslintrc
├── .gitignore
├── .prettierrc
├── Caddyfile
├── docker-compose.yml
├── Dockerfile
├── env.ts
├── index.ts -> entrypoint
├── LICENSE
├── package.json
├── pnpm-lock.yaml
├── README.md
├── render.yaml
├── tsconfig.json
└── vite.config.ts

Microservices

A microservice architecture – a variant of the service-oriented architecture structural style – is an architectural pattern that arranges an application as a collection of loosely-coupled, fine-grained services, communicating through lightweight protocols.

See Micro-Fullstack's Micro Backends for instructions on how to create microservices from source code.

api.example.com
├── /auth
│   └── auth.api.example.com
├── /stats
│   └── stats.api.example.com
├── /todoist
│   └── todoist.api.example.com
├── /feature
│   └── feature.api.example.com
├── /files
│   └── files.api.example.com
└── /settings
    └── settings.api.example.com

Containerization

services:
  - type: web
    name: node-app
    runtime: docker

  - type: web
    name: caddy-server
    runtime: docker