Skip to content

Fast, light-weight and low dependency Express.js multithreaded router

License

Notifications You must be signed in to change notification settings

Trackap/ExpressMultithread

Repository files navigation

Express Multithread Router

Fast, light-weight and low dependency Express.js multithreaded router.

Tests

Installation

This project require typescript and express.

npm i expressmultithread

Usage

  1. Import express & multithreaded router :
// file index.ts
import express from 'express';
import Router from 'expressmultithread';
  1. Create your express app, use router and import controllers :
// file index.ts
const App = express();
App.use("/", Router.router);
Router.importControllers("./controllers");
App.listen(3000, () => console.info("Listening on port 3000"));
  1. Create a controllers directory and create a file 'example.ts' :
// file example.ts
import { controller, route } from 'expressmultithread/dist/decorators';
import { Request, Response } from 'express';

@controller()
export class ExampleController {
    @route("get", "/hello")
    public hello(_req: Request, res: Response) {
        res.status(200).send("Hello world!");
    }
}
  1. Create a configuration file, at root of project :
// file em.config.ts
import { BaseConfig } from "expressmultithread/dist/types";

/* Define a config */
const config : BaseConfig = {
    threadCount: 2 // Define number of thread which run your web server
};

/* Export it as default*/
export default config;

Now you can run index.ts and open the following url http://localhost:3000/hello. If you had trouble with this example you can see example ReadMe.

Too many threads created ? You just have to adjust it, with the configuration file.

API

Imports:
import Router from 'expressmultithread';
import { router, controller } from 'expressmultithread/dist/decorators';
import { ... } from 'expressmultithread/dist/types';
import Config from 'exressmultithread/dist/config';
Prototypes:
Config (Object):

Properties :

  • threadCount (property): Desired number of thread to launch (type: number);
  • plugins (property): Array of path to plugin file (type: string[]);
  • cleanRequest (method): Clean request function (type: (req: Request) => Request);
Router (Object):

Properties :

  • router (property): ExpressRouter to use. (type: ExpressRouter)

Methods :

  • importControllers (method): Define directory to fetch routes (type: (path: string | string[]) => void)
  • use (method): Use a middleware on all routes (type: (path: string, ...args: Serializable[]) => void)
  • unuse (method): Unuse a middleware or remove all used middlewares (type: (middleware?: string, ...args: Serializable[]) => void)

Usefull informations

The object request received in your functions is a light version of the original one, you can customize what's inside by following the overrideReq example.

The object response received in your functions is a total override object, it was overrided following the Express API Documentation 4.x. Please note that transferred informations between threads must be serialisable data, res functions calls give content of the call to the main thread it means you can't pass functions in any res methods.

Middlewares passed in controller or route args, can't be error middleware, this is not handled by the multithreadedrouter, despite, you can handle errors with the main thread following the errorHandling example.

Please note that all path sent in args to plugins are resolve with the current working directory.

Config file

You can a file named em.config.ts which must export as default an object typed with the following type :

import { BaseConfig } from "expressmultithread/dist/types";

In this object you will be able to define different variables :

  • threadCount (number): Number of child thread to launch
  • cleanRequest ((req: Request) => Request): A function which let you pass more args in your Request variables
  • plugins (string[]): Path array of plugin files
  • overrideConsole (boolean): Enable/Disable identifier on console usages
  • debug (boolean): Enable/Disable debug of module
  • verbose (boolean): Enable/Disable extra logging in console
  • restartThreads (boolean): Enable/Disable automatic restart of a new thread in case of crash

More examples

Check examples folder for more examples

License

ExpressMultithread is MIT Licensed