npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

expressmultithread

v1.0.4

Published

Fast, light-weight and low dependency [Express.js](https://www.npmjs.com/package/express) multithreaded router.

Downloads

59

Readme

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