If you are running that script on openshift, you need to listen on port 8080 for it to be externally accessible, or listen on OPENSHIFT_NODEJS_PORT.
How to setup reverse proxy with express-cloudant on openshift hosted nodejs
Question
I ran into the Cross domain policy issue when i wanted to access my couchdb from my website on another host. So I setup a nodejs on openshift to create a reverse proxy with express-cloudant. After modifing the nodejs app example from openshift, I'm currently able to reach the index and readme file which is set via app.get
self.app.get('/', routesDir.index);
self.app.get('/readme', require('./routes/readme'));
But when it comes to the proxy, which should redirect to my cloudant couchdb, I end up with Error 503. This is, how i set up the proxy (from the app.js example in https://github.com/cloudant-labs/express-cloudant)
proxy = require('./routes/proxy'),
http = require('http'),
path = require('path'),
proxy_url = "https://" +
process.env.USERNAME +
".cloudant.com",
admin_url = "https://" +
process.env.USERNAME +
":" +
process.env.PASSWORD +
"@" +
process.env.USERNAME +
".cloudant.com"; self.app = express();
//Cloudant
self.app.set('port', process.env.PORT || 3000);
console.log('Port: ' + self.app.get('port'));
self.app.set('views', __dirname + '/views');
self.app.set('view engine', 'jade');
self.app.use(express.favicon());
self.app.use(express.logger('dev'));
// all requests to /proxy* get forwarded to Cloudant through the proxy
self.app.use(proxy('proxy', proxy_url));
self.app.use(express.bodyParser());
self.app.use(express.methodOverride());
self.app.use(self.app.router);
self.app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == self.app.get('env')) {
self.app.use(express.errorHandler());
}
self.app.get('/', routesDir.index);
self.app.get('/readme', require('./routes/readme'));
require('./routes/api')(self.app, 'api', admin_url);
The content of the proxy.js (which i left unmodified):
module.exports = function(prefix, proxy_url){
return function(req, res, next){
var proxy_path = req.path.match(RegExp("^\\/" + prefix + "(.*)$"));
console.log('Proxy path: ' + proxy_path);
if(proxy_path){
var db_url = proxy_url + proxy_path[1];
req.pipe(request({
uri: db_url,
method: req.method,
qs: req.query
})).pipe(res);
} else {
next();
}
};
};
And my request looks as follows:
curl -vX POST http://myproxy-myappws.rhcloud.com/proxy/_session -H 'application/x-www-form-urlencoded' -d 'name=myuser&password=mypassword'
This ends up with Error 503
When i try the same request with:
curl -vX POST https://me.cloudant.com/_session -H 'application/x-www-form-urlencoded' -d 'name=myuser&password=mypassword'
it works perfectly.
I'm new to nodejs and express-cloudant, so maybe I'm missing a very essential thing. Any infos, directions, hints please?
Solution