1.1.3 • Published 5 years ago
node-express-http-manager v1.1.3
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 defaultconnectionKeyconnection.addConnection(connectionKey, connectionOptions)-- registerconnectionKeyconnection.setDefaultConnection(connectionKey)-- set defaultconnectionKeyconnection.statConnection(connectionKey = null)-- getconnectionOptionsof default orconnectionKeyconnection.connection(connectionKey = null)-- getHttpConnectioninstance of default orconnectionKeyconnection.
Connection Options
string
location-- mandatory location address, for examplehttp://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' */} }