PrimeLens
PrimeLens

Reputation: 2707

In Node JS, how do i create an endpoint pass through? I'm using express and http

In Node JS, how do i create an endpoint pass through? I'm using express and http The entire app will be a just a series of pass through endpoints. Here is my code

// the real endpoint is somewhere else. 
// for example http://m-engine.herokuapp.com/api/getstudents2
var http = require('http');
var options = {
   host: 'http://m-engine.herokuapp.com',
   path: '/api/getstudents2',
   method: 'GET'
};



app.get('/api/getstudents', function(req, res){

    // now past the request through
    http.request(options, function(response2) {
        response2.on('data', function (data) {
            res.json(data);
        });
    }).end();

});

Upvotes: 2

Views: 3045

Answers (1)

Hiren S.
Hiren S.

Reputation: 2832

You can make use bouncy or node-http-proxy module for node.js to achieve the same thing.

Here is the sample code for bouncy:

var fs = require('fs');
var crypto = require('crypto');
var bouncy = require('bouncy');

bouncy(function (req, bounce) {

        bounce("http://m-engine.herokuapp.com");

}).listen(8000);

Although, you can achieve the same thing with help of nginx also. No need to create node service for the same thing. Search on google for nginx proxy_pass. You can get examples for the same.

Upvotes: 1

Related Questions