I'm using a nodejs server to mockup a backend at the moment. The server is a webserver and returns json objects on different requests, works flawlessy. Now I have to get the json objects from another domain, so I have to proxy the server. I have found a package called request in npm. I can get the simple example to work, but I have to forward the whole webpage.
My code for the proxy looks like this:
var $express = require('express'),
$http = require('http'),
$request = require('request'),
$url = require('url'),
$path = require('path'),
$util = require('util'),
$mime = require('mime');
var app = $express();
app.configure(function(){
app.set('port', process.env.PORT || 9090);
app.use($express.bodyParser());
app.use($express.methodOverride());
app.use('/', function(req, res){
var apiUrl = 'http://localhost:9091';
console.log(apiUrl);
var url = apiUrl + req.url;
req.pipe($request(url).pipe(res));
});
});
$http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
if (process.argv.length > 2 && process.argv.indexOf('-open') > -1) {
var open = require("open");
open('http://localhost:' + app.get('port') + '/', function (error) {
if (error !== null) {
console.log("Unable to lauch application in browser. Please install 'Firefox' or 'Chrome'");
}
});
}
})
I'm loggin the real server and it is acting correctly, I can track the get response, but the body is empty. I just want to pass through the whole website from the nodejs server through the request.pipe function. Any ideas?