1.1.3 • Published 4 years ago

node-express-http-manager v1.1.3

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

node-express-http-manager

ExpressJS Http Connections Manager


Usage

const express = require('express');
const HttpManager = require('node-express-http-manager');

const app = express();

app.use((req, res, next) => {
    req.state = {};
    req.httpManager = performHttpConnections();
    return next();
});

// Pass all requests to the php service:
app.use('/', (req, res) => req.httpManager.connection().pass(req, res));

// Pass `/slow` prefixed requests to the slow php service:
app.use('/slow', (req, res) => req.httpManager.connection('php_slow').pass(req, res));

// Use in middleware:
app.use('/hello-world', (req, res,next) => {
    req.httpManager.connection().post('/hello-world/stat-world', {
        id: 'hello'
    }).then((data) => next(data));
});

function performHttpConnections() {
    // Create an instance with a default location:
    const httpManager = new HttpManager('php_common', {
        location: 'http://127.0.0.1:8380'
    });

    // Add a `slow` location:
    httpManager.addConnection('slow', {
        location: 'http://127.0.0.1:8381'
    });

    return httpManager;
}

Methods

  • new HttpManager(connectionKey, connectionOptions) -- create new HttpManager instance and set default connectionKey connection.

  • addConnection(connectionKey, connectionOptions) -- register connectionKey connection.

  • setDefaultConnection(connectionKey) -- set default connectionKey connection.

  • statConnection(connectionKey = null) -- get connectionOptions of default or connectionKey connection.

  • connection(connectionKey = null) -- get HttpConnection instance of default or connectionKey connection.


Connection Options

  • string location -- mandatory location address, for example http://127.0.0.1:4000.

  • function beforePost -- sync post request interceptor:

    {
        beforePost: (requestOpt) => {/* do something with 'requestOpt' */}
    }
  • function beforePass -- sync pass request interceptor:

    {
        beforePass: (requestOpt) => {/* do something with 'requestOpt' */}
    }