Using Node JS to proxy http and modify response
Asked Answered
S

4

8

I'm trying to write a front end to an API service with Node JS.

I'd like to be able to have a user point their browser at my node server and make a request. The node script would modify the input to the request, call the api service, then modify the output and pass back to the user.

I like the solution here (with Express JS and node-http-proxy) as it passes the cookies and headers directly from the user through my site to the api server.

proxy request in node.js / express

I see how to modify the input to the request, but i can't figure out how to modify the response. Any suggestions?

Selway answered 28/11, 2012 at 2:45 Comment(1)
Did you find out how to do this?Nedrud
N
4

Harmon is designed to plug into node-http-proxy https://github.com/No9/harmon It uses trumpet and so is stream based to work around any buffering problems. It uses an element and attribute selector to enable manipulation of a response.

This can be used to modify output response.

See here: https://github.com/nodejitsu/node-http-proxy/issues/382#issuecomment-14895039

Nedrud answered 14/3, 2013 at 10:31 Comment(0)
M
5

transformer-proxy could be useful here. I'm the author of this plugin and I'm answering here because I found this page when looking for the same question and wasn't satisfied with harmon as I don't want to manipulate HTML.

Maybe someone else is looking for this and finds it useful.

Microbicide answered 24/8, 2014 at 11:55 Comment(0)
N
4

Harmon is designed to plug into node-http-proxy https://github.com/No9/harmon It uses trumpet and so is stream based to work around any buffering problems. It uses an element and attribute selector to enable manipulation of a response.

This can be used to modify output response.

See here: https://github.com/nodejitsu/node-http-proxy/issues/382#issuecomment-14895039

Nedrud answered 14/3, 2013 at 10:31 Comment(0)
W
1
var httpProxy = require('http-proxy');
var modifyResponse = require('http-proxy-response-rewrite'); 
var proxy = httpProxy.createServer({
      target:'target server IP here',
        });
  proxy.listen(8001);
  proxy.on('error', function (err, req, res) {
  res.writeHead(500, {
  'Content-Type': 'text/plain'
});
res.end('Something went wrong. And we are reporting a custom error message.');
 });

proxy.on('proxyRes', function (proxyRes, req, res) {

modifyResponse(res, proxyRes.headers['content-encoding'], function (body) {
    if (body && (body.indexOf("<process-order-response>")!= -1)) {
        var beforeTag = "</receipt-text>"; //tag after which u can add data to 
                                                              //       response
        var beforeTagBody = body.substring(0,(body.indexOf(beforeTag) + beforeTag.length));
              var requiredXml = " <ga-loyalty-rewards>\n"+
                                 "<previousBalance>0</previousBalance>\n"+
                                 "<availableBalance>0</availableBalance>\n"+
                                 "<accuruedAmount>0</accuruedAmount>\n"+
                                 "<redeemedAmount>0</redeemedAmount>\n"+                                   
                                 "</ga-loyalty-rewards>";
     var afterTagBody = body.substring(body.indexOf(beforeTag)+  beforeTag.length)+
     var res = [];
     res.push(beforeTagBody, requiredXml, afterTagBody);    
     console.log(res.join(""));
     return res.join("");
    }
    return body;
   });
});
Waly answered 12/1, 2020 at 9:21 Comment(0)
L
0

http-proxy-interceptor is a middleware I wrote for this very purpose. It allows you to modify the http response using one or more transform streams. There are tons of stream-based packages available (like trumpet, which harmon uses), and by using streams you can avoid buffering the entire response.

Liturgics answered 10/10, 2018 at 19:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.