Skip to content

Latest commit

 

History

History
114 lines (81 loc) · 2.23 KB

README.md

File metadata and controls

114 lines (81 loc) · 2.23 KB

Micro Web Server

A simple and opinionated web server powered by Express.

This documentation is a work in progress.

Installation

npm i @guilhermemj/micro-web-server

Simple Usage

Javascript example

var WebServer = require("@guilhermemj/micro-web-server");

var HTTP_PORT = 3000;

var webServer = new WebServer({
  httpPort: HTTP_PORT,
  routes: [
    {
      method: "get",
      path: "/",
      controller: (req, res) => {
        res.send("Hi there!");
      },
    },
  ],
});

webServer.start().then(function () {
  console.log("Web server started at port " + HTTP_PORT);
});

Typescript example

import WebServer, { Request, Response } from "@guilhermemj/micro-web-server";

const HTTP_PORT = 3000;

const webServer = new WebServer({
  httpPort: HTTP_PORT,
  routes: [
    {
      method: "get",
      path: "/",
      controller: (req: Request, res: Response) => {
        res.send("Hi there!");
      },
    },
  ],
});

(async () => {
  await webServer.start();
  console.log(`Web server started at port ${HTTP_PORT}`);
})();

Options

WebServer constructor accepts an object with the following properties:

httpPort

  • Type: number.
  • Default: 3000.

The HTTP port that the server will listen to.

parserOptions

  • Type: BodyParserOptions.
  • Default: undefined.

Options object passed to express.json.

corsOptions

  • Type: CorsOptions.
  • Default: undefined.

Options object passed to cors.

routes

  • Type: Route[].
  • Default: [].

Your application route definitions. Details will be described below.

beforeEach

  • Type: Controller.
  • Default: undefined.

Middleware that should run before each route controller. It's usually used for logging purposes.