express - http proxy in Node.js -
i m trying create http proxy in node.js gets request , makes new request server. purpose getting rid of cross origin problem while , test application. m getting exception :
_stream_readable.js:536 var ret = dest.write(chunk); dest.write not function
i m totally newbie in node.js, on right path ? or there better way ?
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 }); });
to rid of cross origin error, have send access-control header (before data shipped client). purpose 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(); });
Comments
Post a Comment