2.0.1 • Published 7 months ago

@samlior/http-server v2.0.1

Weekly downloads
-
License
GPL-3.0-or-later
Repository
github
Last release
7 months ago

@samlior/http-server

@samlior/http-server is an interruptible HTTP server based on JSONRPC.

Install

npm install @samlior/http-server

Usage

import { HTTPServer, IHTTPHanlder } from "@samlior/http-server";

class MockEchoHandler implements IHTTPHanlder {
  async handle(params: any): Promise<string> {
    if (typeof params !== "string") {
      throw new Error("invalid params");
    }
    return params;
  }
}

const { server, httpServer, terminator } = await HTTPServer.create(
  {
    maxTokens: 10,
  },
  "/",
  8080
);

// register handler
server.register("echo", new MockEchoHandler());

// start
server.start();

process.on("SIGINT", async () => {
  // this function will ensure that all executing requests are completed
  // and safely close all client connections
  await HTTPServer.shutdown(server, terminator);

  // do something...

  process.exit(0);
});