Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to rewrite html body #796

Open
dabeike opened this issue Mar 25, 2015 · 6 comments
Open

how to rewrite html body #796

dabeike opened this issue Mar 25, 2015 · 6 comments

Comments

@dabeike
Copy link

dabeike commented Mar 25, 2015

how to change the contents of html,when the page is received from proxy?
my aim is to add some html code to the page,when the page returns to client
the code is like followings:

proxy.on('proxyRes', function (proxyRes, request, response){
     proxyRes.on('end',function(){
            if(proxyRes.headers['content-type']=='text/html;charset=utf-8'){
                proxyRes.write('hhhhhhhhhhh');   //this sentence may be wrong
            }
        })
})
@betlab
Copy link

betlab commented Mar 26, 2015

+1

@kokarn
Copy link

kokarn commented Mar 26, 2015

I'm not at all sure what's the best way to do this, but this is a scrip we've used successfully to inject a script into a page.

var scriptElm = '<script src="test.js"></script>';

function modifyHtml( str ) {
    // Add or script to the page
    if( str.indexOf( '</body>' ) > -1 ) {
        str = str.replace( '</body>', scriptElm + '</body>' );
    } else if ( str.indexOf( '</html>' ) > -1 ){
        str = str.replace( '</html>', scriptElm + '</html>' );
    } else {
        str = str + scriptElm;
    }

    return str;
}

proxy.on( 'proxyRes', function ( proxyRes, request, response ) {
    if( proxyRes.headers &&
        proxyRes.headers[ 'content-type' ] &&
        proxyRes.headers[ 'content-type' ].match( 'text/html' ) ) {

        var _end = response.end,
            chunks,
            _writeHead = response.writeHead;

        response.writeHead = function(){
            if( proxyRes.headers && proxyRes.headers[ 'content-length' ] ){
                response.setHeader(
                    'content-length',
                    parseInt( proxyRes.headers[ 'content-length' ], 10 ) + scriptElm.length
                );
            }

            // This disables chunked encoding
            response.setHeader( 'transfer-encoding', '' );

            // Disable cache for all http as well
            response.setHeader( 'cache-control', 'no-cache' );

            _writeHead.apply( this, arguments );
        };

        response.write = function( data ) {
            if( chunks ) {
                chunks += data;
            } else {
                chunks = data;
            }
        };

        response.end = function() {
            if( chunks && chunks.toString ) {
                _end.apply( response, [ modifyHtml( chunks.toString() ) ] );
            } else {
                _end.apply( response, arguments );
            }
        };
    }
});

@xtof78
Copy link

xtof78 commented Jun 30, 2015

This solution must be modified to avoid the use of end( ... modify ) which uses implicilty the write() method which has been rerouted into collecting the chunks. So you finish having another chunk which is your own.

Fix:

        var _write = response.write;

...

        response.end = function() {
            if( chunks && chunks.toString )
                _write.apply( this, [ modifyHtml( chunks.toString() ) ] );
            _end.apply( this, arguments );
       };

@macscripter
Copy link

Question, I would like to do the same but for request. That's, before proxyng, I need to put an extra parameter to the body and after proxy the request. How can I do it?

@xingshijie
Copy link

Maybe this method is right for you

proxy.on('proxyRes', function (proxyRes, req, res) {
    var body = new Buffer('test body');
    proxyRes.pipe = function (res) {
      res.write(body)
      res.end()
    }
})

@xianshenglu
Copy link

xianshenglu commented Feb 8, 2023

This works for me. https://github.com/saskodh/http-proxy-response-rewrite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants