I'm new to Node.js and struggling to understand how to create a simple proxy for a given route/url.
I'm trying to avoid cross-domain issues for a front-end javascript logging framework by using a proxy with my node.js (with express) server. I'm using node-http-proxy module but I've run into a problem where the request.body is always JSON. I need to send url-encoded data, not JSON, to the logging server end point. So even if I send url-encoded data as the body of the request to the proxy, the proxy then sends JSON data as the body of the request to the logging server.
I tried converting the body to a url-encoded string in the proxy, but then it even more bizarrely prepends a quote character to the beginning of the body. One single double quote.
Thanks for the help. Here's my route:
this.match('/proxy/logging', function(req, res, next){
req.url = build.logging_url.path;
req.body = require('querystring').stringify(req.body);
req.headers['content-length'] = req.body.length;
proxy.proxyRequest(req, res, {
host: build.logging_url.host
, port: build.logging_url.port
});
}, { via: 'POST' });
and my ajax call, in jquery is:
$.ajax({
type: 'POST',
url: POSTURL,
data: {foobar : "foobar"},
success: on_post_success,
error: on_post_error
});