1

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

Wilm
  • 41
  • 4
  • "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 is not what I intended to do", then what do you want? – Ravi Oct 14 '14 at 11:03
  • I want to see the "a"-pad, as I try to change the url to 'url = "/p/a"'. – Wilm Oct 14 '14 at 11:15
  • what does `console.log(url.slice(0,3))` (before `if(url.slice....`), prints on your browser console? – Ravi Oct 14 '14 at 11:27
  • Well, a lot (as a lot of static traffic is proxyed). So the result is one "/p/", and a lot of "/lo" or "/st" for locales and static stuff. to answer the question: yes, the url rewrite is done. But it seems to have no effect. – Wilm Oct 14 '14 at 11:36
  • **`Hint`**: Apply `urlArr = url.split('/')`, this returns the Array with different index values, use `console.log(urlArr)` to check the values, then apply `if (url[2] == 'p')` to match the url. – Ravi Oct 14 '14 at 11:51
  • thx for the hint. Of course the matching is not very clever ;). However, the problem still exist :( – Wilm Oct 14 '14 at 11:56

1 Answers1

1

well then. I just chatted with etherpad-lite devs. This plan does not work because of the internal working of etherpad. The client asks for data regarding the url it sees in the browser, thus rewrite is useless.

I added this answer if someone else encounter that problem.

Wilm
  • 41
  • 4