简体   繁体   中英

Node.js: using express route for url rewrite to proxy to etherpad

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, eg "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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM