node-express-http-manager
v1.1.3
Published
ExpressJS Http Connections Manager
Downloads
19
Maintainers
Readme
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 defaultconnectionKey
connection.addConnection(connectionKey, connectionOptions)
-- registerconnectionKey
connection.setDefaultConnection(connectionKey)
-- set defaultconnectionKey
connection.statConnection(connectionKey = null)
-- getconnectionOptions
of default orconnectionKey
connection.connection(connectionKey = null)
-- getHttpConnection
instance of default orconnectionKey
connection.
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' */} }