-
Notifications
You must be signed in to change notification settings - Fork 2k
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 modify the response HTML data going back to the user? #382
Comments
This short answer is you can't. You can observe it, but you cannot modify it, e.g. //
// Create a proxy server with custom application logic
//
httpProxy.createServer(function (req, res, proxy) {
//
// Listen to the `proxyResponse` event. Make note that
// the first two arguments to this handler are the `req`
// and `res` in the existing scope and I'm indicating that
// they are meaningless by assigning them the var name `_`
//
req.on('proxyResponse', function (_, _, proxyRes) {
proxyRes.on('data', function (chunk) {
//
// This is the data from the target server, but modifying
// it will not affect the outgoing `res`.
//
});
});
//
// Put other custom server logic here
//
// Now make the proxy request.
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 9000
});
}).listen(8000); In general this is really bad practice unless you can do the modification in a streaming manner because you will have to buffer the entire response. |
Hi there, What I want to do is have a Wordpress website and proxy it with NodeJS, then when a webpage comes back from Wordpress at some particular URL then I want to modify the contents of the webpage to insert a form. The idea is that its slightly difficult to develop in Wordpress and easier to develop in NodeJS (for me). So by proxy Wordpress I thought I can add extra things to my Wordpress page. Wordpress is good for business cases, fast development of site at a cheaper price and easy for user to use as a CRM. Can you suggest a way to go about doing this? I would really appreciate any thoughts you have on this. I want to intercept all incoming and outgoing and modify the request/response data in insert my own data, ie a form. Thanks! Philip |
@PhilHongKong Harmon is designed to plug into node-http-proxy https://github.com/No9/harmon |
Nice. @No9 could you make a pull-request to README.md about this? We get this question a lot. |
Thanks, yes my first thought was to google for a proxy to NodeJS. The question is in general, how to find the Harmon/Trumpet in the first place for a person who is searching for a way to change the data coming out of a web-server passing through the proxy, I didn't find Haron/Trumpet from google. |
Pull done. Also did a little bit of house keeping on the repo readme too. |
How about JSON responses? What what module similar to this can I use to modify JSON responses? |
How about using replacestream? |
Harmon / Trumpet allow you to replace a html element, but what if you want to insert elements at various points in the document being returned? |
Hi @akshayl Does this fit your use case? |
Thanks for your reply @No9 Before: After: |
@akshayl ah now I get you. |
hi guys i am working with the same solution // // // Now make the proxy request. but in sum cases i am getting only half of the data shlomi |
Sorry to hijack this issue, but I want to do exactly what this issue says I should be able to. I want to inject an httpProxy between clients and a JSON API server, only observe the JSON data before passing it back, and perform server-side logic. This seems like it should be easy but it isn't. Here is my code:
I just get a bunch of I'm stuck on Node 0.10.43 if that makes a difference--not my choice, unfortunately. |
And of course 5 minutes after I post that issue, I spot my typo:
I.e. The first parameter to the event callback is a |
I had same problem and only solution I've found I peeped in the very same harmon module: https://github.com/No9/harmon/blob/master/examples/gzipped.js#L47-L64
Or you can just use https://github.com/philippotto/transformer-proxy |
I needed to apply some throttling to node-http-proxy, so not really a transformation of the body but still the best implementation that I could think of is to use a throttle stream (for example node-stream-throttle). As I didn't want a hackish solution, I made a small addition to the options in this fork: koumoul-dev@4db8736 The resTransformStream and reqTransformStream options are exactly what their names suggest: optional transform streams that can be piped between req to proxyReq and proxyRes to res. If some people are interested and the maintainers are ok with the idea I am willing to work on a proper PR, with doc, test, etc. |
Found this package: resp-modifier that works fairly well for this usecase |
Is there still no easy built-in way to send a different response with http-proxy? Use case: the proxy may only know when to replace a response with a 302 redirect based on looking at the content, but with the |
Hi @trusktr did you find a solution for this particular case? Thanks! |
Hi, I really need to know how to get the response HTML as a string and modify it.
This question has been asked before at stackoverflow but I don't see how to do it.
http://stackoverflow.com/questions/13596942/using-node-js-to-proxy-http-and-modify-response
In general, I need to get access to all request data and all response data and change the response data in some cases. Thanks!
The text was updated successfully, but these errors were encountered: