@guilhermemj/micro-web-server
TypeScript icon, indicating that this package has built-in type declarations

1.2.2 • Public • Published

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.

Package Sidebar

Install

npm i @guilhermemj/micro-web-server

Weekly Downloads

3

Version

1.2.2

License

Apache-2.0

Unpacked Size

22.3 kB

Total Files

15

Last publish

Collaborators

  • guilhermemj