1.0.4 • Published 4 years ago

express-power v1.0.4

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

express-power npm

Express with superpowers! Effortless clustering, CLI logging & colors with Express.

Support us on Patreon to get priority updates on our development plan and voting power on new features.

Installation

Install Express Power with yarn or npm:

yarn add express-power express
npm install express-power express

Usage & Examples

Minimal example

const Power = require("express-power");

const worker = app => {
    app.listen(4000, () => Power.log("Listening on port 4000".green));
};

Power.load({ worker });

Extended Example

const Power = require('express-power');
const {log} = Power;

const master = () => {
    log("app started");
};

const worker = app => {
    app.use((req, res) => res.status(200).send("Hello World"));
    app.listen(4000, () => Power.log("Listening on port 4000".green));
};

let options = {
    workers: 8,
    logToFile: true,
    path: __dirname + "/power.log",
    master,
    worker,
};

Power.load(options);

In production, usage of pm2 to run your Express Power app is strongly recommended.

Options

Power options you can use in Power.load(options);

OptionDefault ValueWhat it does
workers2Number of workers.
logToFilefalseEnable logging to file.
pathappDirectory + '/power.log'Log filename.
master() => {}Function to execute before spawning master process. You can welcome the user and run pre-checks here.
worker() => {}Function to execute after spawning worker process. Your Express app should be handled here.
autoRestarttrueRestarts automatically a worker when it dies. Useful for keeping your process alive when unexpected errors occur.

Full Reference

Require Express Power this way:

const Power = require('express-power');

Now, the exported elements are:

const {load, log, italy} = Power;

Let's see them in detail:

ElementWhat it doesProto
loadPowers Expressload(options)
logPower logs to consolelog(text)
italyColors text as Italian flagitaly(text) / text.italy

Power options you can use in load(main, options);

OptionDefault ValueWhat it does
workers2Number of workers.
logToFilefalseEnable logging to file.
pathappDirectory + '/power.log'Log filename.
master() => {}Function to execute before spawning master process. You can welcome the user and run pre-checks here.
worker() => {}Function to execute after spawning worker process. Your Express app should be handled here.
autoRestarttrueRestarts automatically a worker when it dies. Useful for keeping your process alive when unexpected errors occur.

Notice: this package uses Colors internally, which extends the String prototype.

Contributing

Feel free to open an Issue or send me a direct message.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

License

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