Class Server<Options, Middlewares, Context>

Type Parameters

  • const Options extends ServerOptions
  • Middlewares extends UsableMiddleware[] = []
  • Context extends Record<string, any> = {}

Constructors

Properties

Validator: (new <Data>(...args) => default<Data, Context, Middlewares>) = Validator

Type declaration

_status: ServerStatus = 'stopped'
context: Context = ...
implementation: Implementation
interval: null | Timeout = null
middlewares: Middlewares
openAPISchemas: Record<string, SchemaObject | ReferenceObject> = {}
options: {
    bind: string;
    compression: {
        http: {
            enabled: boolean;
            maxSize: number;
            minSize: number;
            preferOrder: CompressionAlgorithm[];
        };
        ws: {
            enabled: boolean;
            maxSize: number;
        };
    };
    logging: {
        debug: boolean;
        error: boolean;
        warn: boolean;
    };
    methods: {
        head: boolean;
        trace: boolean;
    };
    performance: {
        eTag: boolean;
        lastModified: boolean;
    };
    port: number;
    proxy: {
        compress: boolean;
        credentials: {
            authenticate: boolean;
            password: string;
            username: string;
        };
        enabled: boolean;
        force: boolean;
        header: Lowercase<string>;
        ips: {
            list: (Subnet<4 | 6> | IPAddress<4 | 6>)[];
            mode: "whitelist" | "blacklist";
            validate: boolean;
        };
    };
    version: boolean;
}

Type declaration

  • bind: string

    THe IP Address to bind the server to

    Default

    "0.0.0.0"
    

    Since

    0.0.4

  • compression: {
        http: {
            enabled: boolean;
            maxSize: number;
            minSize: number;
            preferOrder: CompressionAlgorithm[];
        };
        ws: {
            enabled: boolean;
            maxSize: number;
        };
    }

    Various Compression-Related Settings

    Since

    9.0.0

    • http: {
          enabled: boolean;
          maxSize: number;
          minSize: number;
          preferOrder: CompressionAlgorithm[];
      }

      HTTP Compression Settings

      Since

      9.0.0

      • enabled: boolean

        Whether HTTP Compression is enabled

        Default

        true
        

        Since

        9.0.0

      • maxSize: number

        The Maximum Size of an Item allowed to be compressed

        Default

        size(10).mb()
        

        Since

        9.0.0

      • minSize: number

        The Minimum Size of an Item allowed to be compressed

        Default

        size(1).kb()
        
      • preferOrder: CompressionAlgorithm[]

        The Order in which to prefer Compression Algorithms, non included wont be used

        Default

        ["brotli", "gzip", "deflate"]
        

        Since

        9.0.0

    • ws: {
          enabled: boolean;
          maxSize: number;
      }

      WebSocket Compression Settings

      Since

      9.0.0

      • enabled: boolean

        Whether WebSocket Compression is enabled

        Default

        true
        

        Since

        9.0.0

      • maxSize: number

        The Maximum Size of an Item allowed to be compressed

        Default

        size(1).mb()
        

        Since

        9.0.0

  • logging: {
        debug: boolean;
        error: boolean;
        warn: boolean;
    }

    Various Logging-Related Settings

    Since

    7.4.0

    • debug: boolean

      Whether to enable DEBUG Logs

      Default

      false
      

      Since

      7.4.0

    • error: boolean

      Whether to enable ERROR Logs

      Default

      true
      

      Since

      7.4.0

    • warn: boolean

      Whether to enable WARN Logs

      Default

      true
      

      Since

      7.4.0

  • methods: {
        head: boolean;
        trace: boolean;
    }

    The HTTP Methods that should automatically be handled for you

    Since

    9.5.0

    • head: boolean

      Whether to automatically handle HEAD Requests

      Default

      true
      

      Since

      9.5.0

    • trace: boolean

      Whether to automatically handle TRACE Requests

      Default

      false
      

      Since

      9.5.0

  • performance: {
        eTag: boolean;
        lastModified: boolean;
    }

    Various Performance-Related Settings

    Since

    6.3.0

    • eTag: boolean

      Whether to include ETag Headers on every request with a direct Body

      Default

      true
      

      Since

      6.3.0

    • lastModified: boolean

      Whether to include Last-Modified Headers on every request that serves a file

      Default

      true
      

      Since

      6.3.0

  • port: number

    The Port to use for the Server, can be random using 0

    Default

    0
    

    Since

    0.0.1

  • proxy: {
        compress: boolean;
        credentials: {
            authenticate: boolean;
            password: string;
            username: string;
        };
        enabled: boolean;
        force: boolean;
        header: Lowercase<string>;
        ips: {
            list: (Subnet<4 | 6> | IPAddress<4 | 6>)[];
            mode: "whitelist" | "blacklist";
            validate: boolean;
        };
    }

    HTTP Proxy Options

    Since

    8.0.0

    • compress: boolean

      Whether to compress the requests

      Default

      false
      

      Since

      9.0.0

    • credentials: {
          authenticate: boolean;
          password: string;
          username: string;
      }

      The Credentials that the proxy will use

      Since

      8.0.0

      • authenticate: boolean

        Whether to authenticate proxy requests

        Default

        false
        

        Since

        8.0.0

      • password: string

        The Password required to authenticate

        Default

        "proxy"
        

        Since

        8.0.0

      • username: string

        The Username required to authenticate

        Default

        "proxy"
        

        Since

        8.0.0

    • enabled: boolean

      Whether to check for proxies and use alternate IPs

      Default

      false
      

      Since

      8.0.0

    • force: boolean

      Whether to force all requests through the proxy

      Default

      false
      

      Since

      8.0.0

    • header: Lowercase<string>

      The Header to use for getting the actual IP address, it must only include the IP address

      Default

      "x-forwarded-for"
      

      Since

      8.0.0

    • ips: {
          list: (Subnet<4 | 6> | IPAddress<4 | 6>)[];
          mode: "whitelist" | "blacklist";
          validate: boolean;
      }

      The IPs the server will validate against

      Since

      9.0.0

      • list: (Subnet<4 | 6> | IPAddress<4 | 6>)[]

        The List of Authorized (or Unauthorized) Proxy IPs

        Default

        [...ReverseProxyIps.LOCAL, ...ReverseProxyIps.CLOUDFLARE]
        

        Since

        9.0.0

      • mode: "whitelist" | "blacklist"

        The Mode in which to validate the list

        Default

        "whitelist"
        

        Since

        9.0.0

      • validate: boolean

        Whether to validate proxy ips

        Default

        false
        

        Since

        9.0.0

  • version: boolean

    Whether to add the rjweb-server header that contains the version

    Default

    true
    

    Since

    9.0.0

promises: Promise<any>[] = []

Accessors

Methods

  • Grab a Channel from either a string identifier or a Channel object

    Type Parameters

    Parameters

    Returns Channel<C>

    Example

    const channel = ctr.$channel('channel')

    await channel.send('text', 'Ok')

    // or

    const ref = new Channel<string>()
    const channel = ctr.$channel(ref)

    await channel.send('text', 'Ok')

    Since

    9.8.1

  • Add a Content Type Mapping to override (or expand) content types

    Parameters

    • callback: ((builder) => default)
        • (builder): default
        • Parameters

          • builder: default

          Returns default

    Returns this

    Since

    5.3.0

  • Fetch (simulate) a request to the Server

    Parameters

    • path: string | URL
    • Optional request: RequestInit

    Returns Promise<Response>

    Since

    9.3.0

  • Generate an OpenAPI Specification for the Server

    Parameters

    • name: string
    • version: string
    • server: ServerObject
    • Optional contact: ContactObject

    Returns OpenAPIObject

    Since

    9.0.0

  • Create a new Path

    Parameters

    • prefix: string
    • callback: ((path) => any)

    Returns this

    Since

    6.0.0

  • Get the Server's Port that its listening on

    Returns null | (Options extends {
            port: number;
        }
        ? Options<Options>["port"] extends 0
            ? number
            : Options<Options>["port"]
        : number)

    Since

    9.0.0

  • Add an OpenAPI Schema to the Server

    Parameters

    • name: string
    • schema: SchemaObject | ReferenceObject

    Returns this

    Since

    9.0.0

  • Start the Server Instance

    Returns Promise<number>

    Example

    import { Server } from "rjweb-server"

    const server = new Server({})

    server.start().then((port) => {
    console.log(`Server Started on Port ${port}`)
    })

    Since

    3.0.0

  • Stop the Server Instance

    Returns this

    Example

    import { Server } from "rjweb-server"

    const server = new Server(...)

    server.start().then((port) => {
    console.log(`Server Started on Port ${port}`)

    setTimeout(() => {
    server.stop()
    console.log('Server Stopped after 5 seconds')
    }, 5000)
    })

    Since

    3.0.0