Archives

This is part IV of the tutorial, and covers: Setting up CORS on Node.js

Once we are done with all the tutorials, you will have a working back-end with Node.js / Express 4 and a working front-end, with Ext JS. Only there will be one problem. The client, can’t reach the JSON backend, because both are running on different ports / or different domains.

Cross-Origin Resource Sharing is a mechanism for allowing clients to interact with APIs that are hosted on a different domain. CORS works by requiring the server to include a specific set of headers that allow a browser to determine if and when cross-domain requests should be allowed. (For more information about CORS, take a look here: http://www.html5rocks.com/en/tutorials/cors/

Let’s open server/package.json, and add the following dependency:

"cors": "^2.7.1",

Open server/config/express.js, and add the following variable to the top of the file:

cors = require('cors'),

In the module.exports function, add the following line:

app.use(cors());

Now save both files, stop Nodemon, and run on the command-line:

$ npm install
$ nodemon app.js

This should fix your cross origin problem.

By the end of this part of the tutorial, your client can reach your server, without cross origin blockages. You learned about CORS.