REserve: Regex-Configurable HTTP Server

2.3.5 · active · verified Wed Apr 22

REserve is a lightweight and versatile HTTP server package, currently at stable version 2.3.5. It specializes in serving static files, acting as a reverse proxy, and aggregating resources from multiple sources. The core functionality relies on declarative, regular expression-based mappings defined in a JSON configuration, offering fine-grained control over request routing and handling. The project demonstrates an active release cadence, with frequent minor updates and bug fixes. Its key differentiators include a minimal footprint with zero runtime dependencies, efficient performance (updated to `punycache`@`1.0.1` in v2.3.0), and the power of embedded regular expressions for highly flexible and dynamic server configurations, making it well-suited for development environments and complex routing scenarios without the overhead of larger frameworks.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to embed and start a `reserve` HTTP server in a TypeScript application with basic static file serving, including event handling for ready, error, and graceful shutdown on process signals.

import { serve, Configuration, Server } from 'reserve';
import * as path from 'path';

const config: Configuration = {
  port: 8080,
  mappings: [
    {
      // Serve files from the current working directory
      match: '^/(.*)',
      file: path.join(process.cwd(), '$1') // Ensures serving from the correct absolute path
    },
    {
      // Default catch-all for unmatched requests
      status: 404
    }
  ]
};

let serverInstance: Server | null = null; // To hold the server instance for graceful shutdown

serve(config)
  .on('ready', ({ url }: { url: string }) => {
    serverInstance = serverInstance; // Capture the instance
    console.log(`REserve server running at ${url}`);
    console.log(`Try accessing http://localhost:${config.port}/package.json if this file exists in your current directory.`);
  })
  .on('error', (error: Error) => {
    console.error('REserve server encountered an error:', error.message);
  })
  .on('abort', () => {
    console.log('REserve server aborted.');
  });

// Implement graceful shutdown on process termination signals
process.on('SIGINT', async () => {
  console.log('SIGINT received. Shutting down REserve server...');
  if (serverInstance) {
    try {
      await serverInstance.close();
      console.log('REserve server gracefully closed.');
    } catch (err) {
      console.error('Error closing REserve server:', err);
    }
  }
  process.exit(0);
});

process.on('SIGTERM', async () => {
  console.log('SIGTERM received. Shutting down REserve server...');
  if (serverInstance) {
    try {
      await serverInstance.close();
      console.log('REserve server gracefully closed.');
    } catch (err) {
      console.error('Error closing REserve server:', err);
    }
  }
  process.exit(0);
});

view raw JSON →