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

simple-express-cluster

v1.0.0

Published

A simple cluster wrapper for node js apps

Downloads

1

Readme

Simple Express Cluster

A simple and light weight library to implement the cluster for your node apps.

Why use simple-express-cluster?

  • Simple and easy to use.
  • Works for both express and native node servers.
  • Auto restart workers during uncaught exception until reach the maximum limit of restart count.
  • Displaying running cluster status. (i.e) Healthcheck, stats.
  • Zero dependancy.
  • Typescript supported.

Installation

To install the stable version:

Using npm as your package manager.

  npm install --save simple-express-cluster

Using yarn as your package manager.

  yarn add simple-express-cluster

Usage

Basic Usage

Example 1

const express = require('express');
const SimpleExpressCluster = require('simple-express-cluster').SimpleExpressCluster;
const simpleExpCluster = new SimpleExpressCluster();
const app = express()
const port = 3000
app.get('/', function (req, res) {
    res.send('Hello World...!').end();
});
simpleExpCluster.run((worker) => {
    console.log(worker.process.pid)
    app.listen(port, () => {
        console.log(`Example app listening at http://localhost:${port}`)
    })
});

Example 2

const express = require('express');
const SimpleExpressCluster = require('simple-express-cluster').SimpleExpressCluster;
const simpleExpCluster = new SimpleExpressCluster({ cpus:2,  auto_restart: true, auto_restart_limit: 3 });
simpleExpCluster.on('output', (message) => console.log(message));
const app = express()
const port = 3000
app.get('/', function (req, res) {
    res.send('Hello World...!').end();
});
simpleExpCluster.setExpress(app);
simpleExpCluster.run((worker) => {
    console.log(worker.process.pid)
    app.listen(port, () => {
        console.log(`Example app listening at http://localhost:${port}`)
    })
});

API

Config

we can configure the default values by passing the config object to simple express cluster contructor

  • cpus - Number of workers.
  • auto_restart - Restart the worker on the process exit
  • auto_restart_limit - Restart limit to avoid the round robin
{
    cpus: 4,                // defaults to os.cpus().length
    auto_restart: false,    // defaults to false
    auto_restart_limit: 3   // defaults to 3
}

run

The run method will initialize cluster based on configuration. The current worker will be received in the argument callback itself.

simpleExpCluster.run((worker) => {
    app.listen(port, () => {
        console.log(`Example app listening at http://localhost:${port}`)
    })
});

Note: Ensure that you have invoked the server listen method in the callback method

setExpress

Set the current express instance in simple express cluster. so that, the healthcheck and stats api will be attached in the given express instance.

simpleExpCluster.setExpress(app);

Note: The setExpress method is only applicable for express servers.

/cluster/healthcheck

Gives the cluster status

curl localhost:3000/cluster/healthcheck
> Simple express cluster is runnging...!

/cluster/stats

Gives the cluster stats. (i.e) Number of worker, worker process ids..etc.

curl localhost:3000/cluster/stats
> {"cluster_size":2,"master_process_id":32183,"cluster_status":"running","workers":[{"worker_id":2,"pid":32191,"status":"online"},{"worker_id":1,"pid":32190,"status":"exit","reason":"The worker died with exit code 0, and signal null"},{"worker_id":3,"pid":32209,"status":"online"}]}

Event

A customer event to notify the verbose logs

simpleExpCluster.on('output', (message) => console.log(message));

License

This project is licensed under the MIT License - see the LICENSE.md file for details.