Elysia Tools

Explora herramientas, ejemplos, explicaciones visuales y guías temáticas en un solo espacio claro.

Categorías populares

Temas

Idioma
ZHESPTRUFRDEEN

Elysia Tools

Navegación

Frameworks web

Ejemplos Fastify: esquemas, totales y errores

Contar usuarios; Comprobar límites; Distinguir errores.

Ejemplos

Entradas de esta colección

CódigojavascriptSimple

Hola Mundo con Fastify

Configuración básica del servidor Fastify con rutas, middleware y características esenciales para aplicaciones web de alto rendimiento

javascript
// Fastify Hello World Examples

// 1. Basic Fastify Server
var fastify = require('fastify')({ logger: true });

// Declare a route
fastify.get('/', async (request, reply) => {
  return { hello: 'world' };
});

// Run the server
var start = async () => {
  try {
    await fastify.listen(3000);
    console.log('Server listening on http://localhost:3000');
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};

start();

// 2. Fastify with TypeScript
// src/server.ts
import fastifyTs from 'fastify';
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';

const server: FastifyInstance = fastifyTs({
  logger: {
    level: 'info',
    prettyPrint: true,
  },
});

// Define routes
server.get('/', async (request: FastifyRequest, reply: FastifyReply) => {
  return {
    message: 'Hello Fastify with TypeScript!',
    timestamp: new Date().toISOString(),
    version: '1.0.0'
  };
});

server.get('/health', async (request: FastifyRequest, reply: FastifyReply) => {
  return {
    status: 'ok',
    uptime: process.uptime(),
    memory: process.memoryUsage()
  };
});

// Start server
var start = async () => {
  try {
    const port = parseInt(process.env.PORT || '3000');
    const host = process.env.HOST || '0.0.0.0';

    await server.listen({ port, host });
    console.log(`🚀 Server ready at http://${host}:${port}`);
  } catch (err) {
    server.log.error(err);
    process.exit(1);
  }
};

start();

// 3. Fastify with Route Parameters and Validation
var fastify = require('fastify')({ logger: true });

// User data store (in production, use a database)
const users = [
  { id: 1, name: 'John Doe', email: '[email protected]', age: 30 },
  { id: 2, name: 'Jane Smith', email: '[email protected]', age: 25 },
  { id: 3, name: 'Bob Johnson', email: '[email protected]', age: 35 }
];

// Define schemas for validation
const userSchema = {
  type: 'object',
  properties: {
    id: { type: 'number' },
    name: { type: 'string', minLength: 1 },
    email: { type: 'string', format: 'email' },
    age: { type: 'number', minimum: 0, maximum: 150 }
  },
  required: ['name', 'email']
};

const createUserSchema = {
  type: 'object',
  properties: {
    name: { type: 'string', minLength: 1 },
    email: { type: 'string', format: 'email' },
    age: { type: 'number', minimum: 0, maximum: 150 }
  },
  required: ['name', 'email']
};

// Routes with validation
fastify.get('/api/users', {
  schema: {
    description: 'Get all users',
    tags: ['users'],
    response: {
      200: {
        type: 'object',
        properties: {
          users: {
            type: 'array',
            items: userSchema
          },
          total: { type: 'number' }
        },
        required: ['users', 'total']
      }
    }
  }
}, async (request, reply) => {
  return { users, total: users.length };
});

fastify.get('/api/users/:id', {
  schema: {
    description: 'Get user by ID',
    tags: ['users'],
    params: {
      type: 'object',
      properties: {
        id: { type: 'number' }
      }
    },
    response: {
      200: userSchema,
      404: {
        type: 'object',
        properties: {
          error: { type: 'string' }
        }
      }
    }
  }
}, async (request, reply) => {
  const { id } = request.params;
  const user = users.find(u => u.id === id);

  if (!user) {
    reply.code(404).send({ error: 'User not found' });
    return;
  }

  return user;
});

fastify.post('/api/users', {
  schema: {
    description: 'Create a new user',
    tags: ['users'],
    body: createUserSchema,
    response: {
      201: userSchema,
      400: {
        type: 'object',
        properties: {
          error: { type: 'string' }
        }
      }
    }
  }
}, async (request, reply) => {
  const newUser = {
    id: users.length + 1,
    ...request.body
  };

  users.push(newUser);
  reply.code(201).send(newUser);
});

// 4. Fastify with Hooks and Middleware
var fastify = require('fastify')({ logger: true });

// Global hooks
fastify.addHook('onRequest', async (request, reply) => {
  const start = Date.now();
  request.startTime = start;
});

fastify.addHook('onResponse', async (request, reply) => {
  const duration = Date.now() - request.startTime;
  fastify.log.info(`Request to ${request.url} took ${duration}ms`);
});

// Authentication hook
fastify.addHook('preHandler', async (request, reply) => {
  // Skip auth for certain routes
  if (request.url.startsWith('/public') || request.url === '/') {
    return;
  }

  const authHeader = request.headers.authorization;
  if (!authHeader || !authHeader.startsWith('Bearer ')) {
    reply.code(401).send({ error: 'Unauthorized' });
    return;
  }

  // In production, verify JWT token here
  const token = authHeader.substring(7);
  if (token !== 'valid-token') {
    reply.code(401).send({ error: 'Invalid token' });
    return;
  }

  // Add user info to request
  request.user = { id: 1, name: 'Authenticated User' };
});

// Public routes
fastify.get('/public/info', async (request, reply) => {
  return { message: 'This is public information', timestamp: new Date().toISOString() };
});

// Protected routes
fastify.get('/protected/data', async (request, reply) => {
  return {
    message: 'This is protected data',
    user: request.user,
    timestamp: new Date().toISOString()
  };
});

// 5. Fastify with Plugins
// plugins/auth.js
const fp = require('fastify-plugin');

async function authPlugin(fastify, options) {
  fastify.decorate('authenticate', async (request, reply) => {
    try {
      await request.jwtVerify();
    } catch (err) {
      reply.send(err);
    }
  });
}

module.exports = fp(authPlugin);

// plugins/database.js
const fp = require('fastify-plugin');

async function databasePlugin(fastify, options) {
  // Mock database connection
  const db = {
    users: [],
    posts: [],

    async getUser(id) {
      return this.users.find(user => user.id === id);
    },

    async createUser(userData) {
      const user = { id: this.users.length + 1, ...userData };
      this.users.push(user);
      return user;
    }
  };

  fastify.decorate('db', db);
}

module.exports = fp(databasePlugin);

// Main server with plugins
var fastify = require('fastify')({ logger: true });

// Register plugins
fastify.register(require('fastify-jwt'), { secret: 'supersecret' });
fastify.register(require('./plugins/auth'));
fastify.register(require('./plugins/database'));

// Routes using plugins
fastify.get('/api/profile', {
  preHandler: [fastify.authenticate]
}, async (request, reply) => {
  const user = await fastify.db.getUser(request.user.id);
  return { user };
});

fastify.post('/api/users', async (request, reply) => {
  const user = await fastify.db.createUser(request.body);
  return user;
});

// 6. Fastify with File Uploads
var fastify = require('fastify')({ logger: true });
const path = require('path');
const fs = require('fs');
const { pipeline: pump } = require('stream/promises');

// Register multipart plugin for file uploads
fastify.register(require('fastify-multipart'));

// Ensure upload directory exists
const uploadDir = path.join(__dirname, 'uploads');
fs.promises.mkdir(uploadDir, { recursive: true }).catch(console.error);

// File upload route
fastify.post('/upload', async (request, reply) => {
  try {
    const data = await request.file();

    if (!data) {
      reply.code(400).send({ error: 'No file uploaded' });
      return;
    }

    // Generate unique filename
    const filename = `${Date.now()}-${data.filename}`;
    const filepath = path.join(uploadDir, filename);

    // Save file
    await pump(data.file, fs.createWriteStream(filepath));

    reply.send({
      message: 'File uploaded successfully',
      filename,
      size: data.file.bytesRead
    });
  } catch (error) {
    fastify.log.error(error);
    reply.code(500).send({ error: 'File upload failed' });
  }
});

// Multiple files upload
fastify.post('/upload/multiple', async (request, reply) => {
  try {
    const files = [];
    const parts = request.files();

    for await (const part of parts) {
      const filename = `${Date.now()}-${part.filename}`;
      const filepath = path.join(uploadDir, filename);

      await pump(part.file, fs.createWriteStream(filepath));

      files.push({
        filename,
        originalName: part.filename,
        size: part.file.bytesRead
      });
    }

    reply.send({
      message: 'Files uploaded successfully',
      files,
      count: files.length
    });
  } catch (error) {
    fastify.log.error(error);
    reply.code(500).send({ error: 'Multiple file upload failed' });
  }
});

// 7. Fastify with Error Handling
var fastify = require('fastify')({ logger: true });

// Custom error class
class AppError extends Error {
  constructor(message, statusCode) {
    super(message);
    this.statusCode = statusCode;
    this.isOperational = true;
  }
}

// Set error handler
fastify.setErrorHandler(function (error, request, reply) {
  // Log error
  this.log.error(error);

  // Send error response
  if (error.validation) {
    // Validation error
    reply.code(400).send({
      error: 'Validation Error',
      details: error.validation,
      statusCode: 400
    });
    return;
  }

  if (error.isOperational) {
    // Operational error (expected)
    reply.code(error.statusCode || 500).send({
      error: error.message,
      statusCode: error.statusCode || 500
    });
  } else {
    // Programming error
    reply.code(500).send({
      error: 'Internal Server Error',
      statusCode: 500
    });
  }
});

// Route that throws different errors
fastify.get('/error/validation', {
  schema: {
    querystring: {
      type: 'object',
      properties: {
        name: { type: 'string', minLength: 3 }
      },
      required: ['name']
    }
  }
}, async (request, reply) => {
  // This will cause validation error if name is too short
  return { message: `Hello ${request.query.name}` };
});

fastify.get('/error/operational', async (request, reply) => {
  throw new AppError('This is an operational error', 400);
});

fastify.get('/error/programming', async (request, reply) => {
  // This will be caught as a programming error
  throw new Error('Unexpected error occurred');
});

// 8. Fastify with Environment Configuration
var fastify = require('fastify')({
  logger: {
    level: process.env.LOG_LEVEL || 'info'
  }
});

// Configuration
const config = {
  port: parseInt(process.env.PORT || 3000),
  host: process.env.HOST || '0.0.0.0',
  database: {
    url: process.env.DATABASE_URL || 'mongodb://localhost:27017/fastify',
    name: process.env.DATABASE_NAME || 'fastify_db'
  },
  jwt: {
    secret: process.env.JWT_SECRET || 'fallback-secret-key'
  },
  cors: {
    origin: process.env.CORS_ORIGIN || '*',
    credentials: true
  }
};

// Register environment-specific plugins
if (process.env.NODE_ENV === 'development') {
  fastify.register(require('fastify-cors'), config.cors);
}

// Health check endpoint
fastify.get('/health', async (request, reply) => {
  return {
    status: 'ok',
    environment: process.env.NODE_ENV || 'development',
    uptime: process.uptime(),
    memory: process.memoryUsage(),
    config: {
      port: config.port,
      databaseName: config.database.name
    }
  };
});

// 9. Fastify with Testing Setup
// test/server.test.js
const { test } = require('tap');
const build = require('../server');

test('GET / route', async t => {
  const app = build();

  const response = await app.inject({
    method: 'GET',
    url: '/'
  });

  t.equal(response.statusCode, 200);
  t.same(response.json(), { hello: 'world' });
});

test('POST /api/users with valid data', async t => {
  const app = build();

  const userData = {
    name: 'Test User',
    email: '[email protected]',
    age: 25
  };

  const response = await app.inject({
    method: 'POST',
    url: '/api/users',
    payload: userData
  });

  t.equal(response.statusCode, 201);
  t.match(response.json(), userData);
});

test('POST /api/users with invalid data', async t => {
  const app = build();

  const invalidData = {
    name: '',  // Invalid: empty string
    email: 'invalid-email'  // Invalid: not a valid email
  };

  const response = await app.inject({
    method: 'POST',
    url: '/api/users',
    payload: invalidData
  });

  t.equal(response.statusCode, 400);
  t.ok(response.json().validation);
});

// 10. Fastify Production Best Practices
var fastify = require('fastify')({
  logger: {
    level: process.env.LOG_LEVEL || 'info',
    stream: process.stdout
  },
  // Enable trust proxy for proper IP logging behind reverse proxy
  trustProxy: true
});

// Graceful shutdown
process.on('SIGINT', async () => {
  fastify.log.info('Received SIGINT, shutting down gracefully...');
  await fastify.close();
  process.exit(0);
});

process.on('SIGTERM', async () => {
  fastify.log.info('Received SIGTERM, shutting down gracefully...');
  await fastify.close();
  process.exit(0);
});

// Health check for load balancers
fastify.get('/health', {
  schema: {
    hide: true  // Hide from documentation
  }
}, async (request, reply) => {
  return { status: 'ok' };
});

// Production-ready startup
var start = async () => {
  try {
    const port = parseInt(process.env.PORT || 3000);

    await fastify.listen({
      port,
      host: '0.0.0.0'  // Listen on all interfaces
    });

    fastify.log.info(`🚀 Server ready on port ${port}`);
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};

start();

Herramientas

Herramientas que suelen usarse con este ejemplo

Relacionado

Colecciones de ejemplos relacionadas

Relacionado

Temas relacionados

Guía de uso de la herramienta

Contexto para entender esta colección de ejemplos

Resumen

La tarjeta inicial reúne servidores independientes. Lea validación y errores por separado, sin ejecutar todo el archivo mostrado.

Leer una sección cada vez

Tarjetas de esta página: Hola Mundo con Fastify.

Cómo funciona

  1. 1Abra la tarjeta indicada y lea o copie el código en un editor local. Localice las secciones siguientes; la página muestra código, no una demostración activa.

Casos límite y errores frecuentes

  • Alcance de los ejemplos

    Son deducciones del código, no pruebas HTTP. La carga de archivos necesita la corrección adjunta de importaciones de flujos; versiones y despliegue no probados.

Ejemplos

1. Contar usuarios

Cómo usarlo

Hola Mundo con Fastify — En la sección 3, examine GET /api/users con tres registros iniciales y luego un POST válido.

Resultado

total empieza en 3. Tras push, el siguiente cálculo da 4; es estado en memoria, no persistencia en una base de datos.

2. Comprobar límites

Cómo usarlo

Hola Mundo con Fastify — En createUserSchema, compare age 0, 150 y 151 con name y email presentes.

Resultado

0 y 150 están incluidos; 151 queda fuera. name y email son obligatorios; age es opcional.

3. Distinguir errores

Cómo usarlo

Hola Mundo con Fastify — En la sección 7, compare un error con validation, AppError con statusCode 400 y Error normal.

Resultado

Las ramas eligen 400, 400 y 500. validation se revisa primero; el error normal oculta el mensaje original.

Tema

Captura autorizada de webhooks y revisión de confianza del endpoint

Captura y repite solicitudes webhook controladas, revisa la evidencia de entrega y comprueba el certificado HTTPS del receptor sin confundir un certificado válido con confianza empresarial.

Tema

Depuracion de Observabilidad con Logs, Trazas, Webhooks y Payloads JSON

Parsea logs, decodifica trazas, revisa evidencia webhook/API, explora payloads JSON y extrae patrones recurrentes antes de redactar una nota de incidente.

Tema

Conversión de formatos para entrega de video: audio, streams, GIF, WebM y MP4

Define el destino de entrega, extrae solo el stream necesario y convierte animaciones breves, dejando constancia de la compatibilidad, la calidad y el tamaño resultantes.

Tema

Revisión de notas de versión para web, Slack y PDF

Revisa una versión frente a su registro de cambios, valida la sintaxis y las referencias de Markdown y prepara notas coherentes para web, Slack y PDF.

Inicio
Herramientas3216
Ejemplos593
Visualizaciones473
Visores de archivos103
Temas239
Etiquetas333
Imágenes, audio y video657

Herramientas de procesamiento, conversión y optimización de imágenes, audio y video

Matemáticas, fechas y finanzas647

Calculadoras, herramientas numéricas, lógica de fechas, estadística y finanzas

Diseño y color298

Herramientas de color, maquetación, gráficos, estilo visual y apoyo al diseño

Texto y redacción209

Herramientas de redacción, limpieza, formato, extracción y análisis de texto

Desarrollo y Web194

Utilidades para desarrolladores, red, depuración web y automatización

Conversión y codificación175

Herramientas de conversión de formatos, archivos, unidades, archivos comprimidos y codificación

Lanzamiento de un cuestionario de captación y entrega de contactos
Textos conmemorativos: revisión familiar y entrega para impresión
Hábitos privados: copia, restauración y revisión antes de compartir
De artículo a borradores de X y LinkedIn: revisión factual y entrega
Revisión de una lección Markdown interactiva y entrega sin conexión

© 2026 Elysia Tools. Todos los derechos reservados.

DocumentaciónPreciosAcerca deTérminos de servicioPolítica de privacidadCookiesContacto

Media

Optimizador AVIF

Optimizar imágenes AVIF para mejor compresión y rendimiento manteniendo la calidad visual

Frameworks web

Ejemplos de Express.js

Ejemplos del framework web Express.js incluyendo Hello World, routing, middleware y API REST

Frameworks web

Ejemplos de Fiber

Ejemplos del framework web Go Fiber incluyendo enrutamiento, middleware, plantillas, WebSocket, integración de base de datos y optimización de rendimiento

Frameworks web

Ejemplos de Hono

Ejemplos del framework web ultra rápido Hono incluyendo routing, middleware y despliegue

Frameworks web

Ejemplos de NestJS

Ejemplos del framework enterprise Node.js NestJS incluyendo módulos, controladores, servicios y despliegue

Validación

Alternativas de Patrones Regex

Múltiples formas de escribir el mismo patrón regex con diferentes compensaciones

Frameworks web

Ejemplos de Elysia.js

Ejemplos del framework web TypeScript Elysia.js incluyendo Hello World, ruteo, validación y desarrollo de plugins