Reputation: 363
I m trying to create a http proxy in node.js which gets the request and makes a new request to the server. My purpose is getting rid of cross origin problem for a while and test my application. But i m getting exception :
_stream_readable.js:536
var ret = dest.write(chunk);
dest.write is not a function
I m totally newbie in the node.js, so am i on the right path ? or is there a better way to do it ?
app.get('/tlq', (req, res) => {
console.log('serve: ' + req.url);
var options = {
hostname: 'www.google.com',
port: 80,
path: req.url,
method: 'GET'
};
var proxy = http.request(options, function (res) {
res.pipe(res, {
end: true
});
});
req.pipe(proxy, {
end: true
});
});
Upvotes: 0
Views: 133
Reputation: 1009
To get rid of cross origin error, you have to send Access-Control header (before any data is shipped to client). For this purpose you could use next middleware:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
Upvotes: 1