node-get-post-proxy

0.0.2 • Public • Published

#NodeGetPostProxy

When to use NodeGetPostProxy

You have a front-end application which always connect to an IP address and you have multiple server instances:

  • production: the higher percentage of users connect to it
  • development: you want to connect to it to test new features (but you cannot modify the frontend because it is mantained by another team)
  • staging: you want that a sample of the users connect to it in order to do an A/B testing

Your application has a login. You want to decide to which server a user connect basing on its username.

The solution with this module is to let the production server to act as a Proxy. If the user who does the request has to connect to the staging server, because its field "server" in the database is set to "staging", the production server does the request for him (with the get or post fields) and it will return the response.

The module automatically understand if the request was a GET or a POST and forward the GET or POST fields.

Installing NodeGetPostProxy

npm install nodegetpostproxy

How to use it

var express = require('express');
var app = express();
var http = require('http');
var httpProxy = require('NodeGetPostProxy');


var proxy = httpProxy.createProxyServer({
  target: "http://...." //link of the development server
});


app.use(function(req,res,next){
    if (req.params('user_name') == 'Pippo'){
      proxy.web(req,res);
    } else {
      return next();
    }
});

http.createServer(app).listen(80);
console.log('Listening on port 80...');

Run the example

You can find an example in the folder "example".

In 3 different terminal windows run:

  • node app.js
  • node app_development.js
  • node app_staging.js

Go to the browser:

With the link http://localhost will response the production server With the link http://localhost/?user_name=Pippo will response the development server With the link http://localhost/?user_name=Mario will response the staging server

Package Sidebar

Install

npm i node-get-post-proxy

Weekly Downloads

1

Version

0.0.2

License

ISC

Last publish

Collaborators

  • mikspi