my first question on stackoverflow ;).
What I try to do: I want to use an express route "/pad/*" to show etherpads (etherpad lite) and manipulate what pad the user will see. So, if I get "/pad/p/something", the "something" will be processed, and changed accordingly to the real ID, e.g. "XYZ". So the user can edit the correct pad. However, the etherpad needs a lot of static stuff and websockets, thus I want to proxy it, and manipulate the ID if I need to
The minimal working example: of course the real logic of ID changing is much more complicate, but for the minimal working example I just use the logic that every padID is mapped to "a".
I came up with this:
/* packages */
var express = require( "express" );
var http = require( "http" );
var httpProxy = require('http-proxy');
/* app */
var app = express();
app.use(app.router);
/* some express logic */
app.get( '/' , function( req , res ) {
res.end( "hello world" );
} );
/* the proxy */
var padProxy = httpProxy.createProxyServer( {} );
padProxy.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('some error');
});
app.all( '/pad/*' , function( req , res ) {
var url = req.url;
url = url.slice(4)
/*WHY DOES THIS NOT WORK????*/
if( url.slice(0,3) === '/p/' ) {
url = "/p/a"
};
req.url = url;
return padProxy.web(req, res , { target: "http://<etherpad server ip>:<etherpad server port>" } );
} )
/* run */
http.createServer(app).listen( 3000 , function(){
console.log( "started" );
});
The Problem: The url change and the proxying actually works ... kind of. All urls are mapped to remove the "/pad" at the front of every url which works for the pads, the static stuff and the websockets. BUT, the mapping of the pad ID doesn't work. If I look at "localhost:3000/pad/p/a", I see the "a"-pad. If I look at "localhost:3000/pad/p/b", I see the "b"-pad, which ist not what I intended to do :/.
What am I doing wrong? Is this a node-http-proxy, express or etterpad-lite problem?
Any hint is appreciated