Skip to content

naweeyy/fastify-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Fastify API TypeScript Starter

πŸš€ A modern and performant starter for building REST APIs with Fastify and TypeScript.

✨ Features

  • ⚑ Fastify - Ultra-fast web framework
  • πŸ”· TypeScript - Static typing and better DX
  • πŸ”§ Hot Reload - Automatic reload in development
  • πŸ“ Modular structure - Clean organization of plugins and routes
  • πŸ›‘οΈ Type Safety - Custom TypeScript declarations

πŸš€ Quick Start

Prerequisites

  • Node.js (version 18 or higher)
  • npm or yarn

Installation

# Clone the project
git clone <your-repo>
cd fastify-api-ts

# Install dependencies
npm install

Development

# Run in development mode (hot reload)
npm run dev

The API will be available at http://localhost:8888

Production

# Build the project
npm run build

# Run in production
npm start

πŸ“‹ Available Scripts

Script Description
npm run dev Starts the server in development mode with hot reload
npm run build Compiles TypeScript to JavaScript in /dist
npm start Starts the server in production mode
npm test Runs tests (to be configured)

πŸ“ Project Structure

src/
β”œβ”€β”€ app.ts              # Fastify application configuration
β”œβ”€β”€ server.ts           # Server entry point
β”œβ”€β”€ plugins/            # Custom Fastify plugins
β”‚   β”œβ”€β”€ sensible.ts     # Plugin for HTTP errors
β”‚   └── support.ts      # Global utilities
β”œβ”€β”€ routes/             # Route definitions
β”‚   β”œβ”€β”€ root.ts         # Root route
β”‚   └── example/        # Example routes
└── types/              # TypeScript declarations
    └── fastify.d.ts    # Fastify type extensions

πŸ”Œ Adding New Routes

  1. Create a file in src/routes/
  2. Export a Fastify plugin function:
import { FastifyInstance } from 'fastify';

export default async function (fastify: FastifyInstance) {
  fastify.get('/hello', async (request, reply) => {
    return { message: 'Hello World!' };
  });
}
  1. Routes are automatically loaded by Fastify

πŸ› οΈ Customization

Plugins

Add your plugins in src/plugins/ and register them in src/app.ts.

Environment Variables

Create a .env file at the root:

PORT=8888
NODE_ENV=development

πŸ“š Resources

πŸ“ License

MIT

About

πŸš€ API built with the Fastify framework, optimized and ready to use

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks