user288093 user288093 - 3 months ago 31
Node.js Question

write a reverse proxy in node js to call the api in django

My frontend code is running in angular at node httpserver port 127.0.0.1:8081
My backend services runnning in python django framework at port 127.0.0.1:9000
While calling my backend servies from angular http methods throws cors exception
so i wrote a proxy controller in my node js

var http = require('http'),
httpProxy = require('http-proxy');

var proxy = httpProxy.createProxyServer();

http.createServer(function (req, res) {
// This simulates an operation that takes 500ms to execute
setTimeout(function () {
proxy.web(req, res, {
target: 'http://127.0.0.1:9000/dummy/'
});
}, 500);
}).listen(8080, "127.0.0.1");


to listen and bind at angular. i run as the node proxycontroller.js, results a another port no 127.0.0.1:8080

from proxy controller it calls my backend service and result json but from the angular js calling the proxy controller from the http.get() method results cors problem

please help to solve this problem.

Answer

Enable CORS in Django

Uses this third-party lib django-cors to do it.

You are getting CORS because you are making the call from your AngularJS app and its host is 127.0.0.1:8081, different from your Django app host 127.0.0.1:9000