GithubHelp home page GithubHelp logo

nodegetpostproxy's Introduction

#NodeGetPostProxy

When to use NodeGetPostProxy

Problem:

Suppose you have a mobile application in which the back-end address is hardcoded. Suppose you also have multiple server instances:

  • production: users connect to it
  • development: developers connect to it to test new features (to connect to it you have to change the hardcoded back-end address in the mobile application)
  • staging: you want that a sample of the users connect to it in order to do an A/B testing

Solution:

Your application has a login. You can 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();
    }
});

app.get('/', function (req, res) {
  res.send('I am the production server!')
})

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

nodegetpostproxy's People

Contributors

mikspi avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.