Skip to content

Instantly share code, notes, and snippets.

@1stvamp
Created October 18, 2012 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1stvamp/ac8d76e005708b4154ec to your computer and use it in GitHub Desktop.
Save 1stvamp/ac8d76e005708b4154ec to your computer and use it in GitHub Desktop.
http-proxy using nextTick to perform task before proxy
var httpProxy = require('http-proxy');
var http = require('http');
var config = {
"silent": false,
"router": {
"svc.honshuu.dev/alerts/": "alerts.honshuu.dev",
"svc.honshuu.dev/cloud/": "cloud.honshuu.dev:8088",
"svc.honshuu.dev/inventory/": "inventory.honshuu.dev",
"svc.honshuu.dev/users/": "users.honshuu.dev",
"svc.honshuu.dev/metrics/": "metrics-svc",
"svc.honshuu.dev/notifications/": "notifications.honshuu.dev"
},
"changeOrigin": true
};
var proxy = new httpProxy.RoutingProxy(config);
var app = http.createServer();
var server = app.listen(8089, '127.0.0.1');
var task = function(request, response, next) {
// Do something like insert data from Mongo
console.log('Running task..');
next();
}
server.on('request', function(request, response) {
var next = function() {
console.log('Proxying request..');
proxy.proxyRequest(request, response);
};
console.log('Queueing task..');
process.nextTick(function() { task(request, response, next); });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment