Super Simple Fastify Server
This library, `super-simple-fastify-server`, currently at version 1.0.0, provides a lightweight and opinionated wrapper around the Fastify web framework. It is designed for rapidly spinning up temporary or local development servers with minimal boilerplate and configuration, making it ideal for testing, prototyping, or quickly standing up mock APIs. The package's primary goal is to simplify server creation by abstracting common Fastify setup, allowing developers to focus solely on defining routes. It features configurable host and port settings that can be controlled via constructor options or environment variables, prioritizing ease of use over extensive customization. While new and without an established release cadence, it offers a stable API for its current scope and ships with TypeScript type definitions for enhanced developer experience. A key differentiator is its minimal footprint, leveraging Fastify and `pino-pretty` as peer dependencies, giving users control over specific versions of these core components.
Common errors
-
Error: Cannot find module 'fastify' from '<path_to_your_project>'
cause `fastify` peer dependency is not installed.fixInstall Fastify: `npm install fastify` -
Error: listen EADDRINUSE: address already in use :::3456
cause Another process is already using the configured port (default is 3456).fixChange the `port` option in the `SimpleFastifyServer` constructor or set the `SIMPLE_FASTIFY_SERVER_PORT` environment variable to an available port. -
TypeError: (0, _super_simple_fastify_server__WEBPACK_IMPORTED_MODULE_0__.SimpleFastifyServer) is not a constructor
cause Attempting to import `SimpleFastifyServer` as a default export when it is a named export.fixUse a named import: `import { SimpleFastifyServer } from 'super-simple-fastify-server';`
Warnings
- gotcha This library lists `fastify` and `pino-pretty` as peer dependencies. You must install them separately alongside `super-simple-fastify-server` for the server to function correctly.
- gotcha Configuration options (host, port) passed to the `SimpleFastifyServer` constructor can be overridden by environment variables (`SIMPLE_FASTIFY_SERVER_HOST`, `SIMPLE_FASTIFY_SERVER_PORT`) if not explicitly provided, which might lead to unexpected server addresses or ports.
- gotcha This package is explicitly designed for 'rapid dev/test' and does not implement production-grade security, performance optimizations, or robust error handling expected in public-facing applications.
Install
-
npm install super-simple-fastify-server -
yarn add super-simple-fastify-server -
pnpm add super-simple-fastify-server
Imports
- SimpleFastifyServer
import SimpleFastifyServer from 'super-simple-fastify-server';
import { SimpleFastifyServer } from 'super-simple-fastify-server'; - FastifyInstance
import { FastifyInstance } from 'fastify';import { FastifyInstance } from 'super-simple-fastify-server'; - buildObjectStreamResponse
const { buildObjectStreamResponse } = require('super-simple-fastify-server');import { buildObjectStreamResponse } from 'super-simple-fastify-server';
Quickstart
import { FastifyInstance, SimpleFastifyServer } from 'super-simple-fastify-server';
const server = new SimpleFastifyServer(
async (app: FastifyInstance) => {
app.get('/hello-world', async (request, _reply) => {
return { message: `Hello, ${request.query.target}!` };
});
},
{
host: '127.0.0.1',
port: 3456
}
);
(async () => {
await server.start();
console.log(`Server listening at http://${server.config.host}:${server.config.port}`);
// To stop the server later:
// await server.stop();
})();