How to use REST with Node.js and AngularJS being on different port?

By : Michal
Source: Stackoverflow.com
Question!

I am trying to udnerstand cooperation of Angular and Node.js. However I can't figure out, how to use REST for data transfer. I have Node.js backend running on port 8000, responding to various GETs and POSTs. My Angular frontend is loaded through Apache, running on port 80. Naturally, the JavaScript console identifies it as not allowed by Access-Control-Allow-Origin.

How is this commonly solved? Am I doing it wrong? Should I not run the frontend through Apache?

By : Michal


Answers

Add this to your Node.js API server

server.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'OPTIONS,GET,POST,PUT,DELETE');
    res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
    if ('OPTIONS' == req.method) {
        return res.send(200);
    }
    next();
});


One way to solve it is using Cross-origin Resource Sharing. Browser support is decent as well.

By : NilsH


This video can help you solving your question :)
By: admin