Tuesday, May 20, 2014

Setting CORS (cross origin resource sharing) headers in node.js express app

There is a question: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs





The essential part was here:

app.configure(function() {
// has to be in that order ! ! !
app.use(cors());
app.use(express.static(__dirname + '/static'));
 
// will NOT work like that
// for files served statically
// (it took a little while to figure it out)
// app.use(express.static(__dirname + '/static'));
// app.use(cors());
})
I encourage you to use Nitrous - reflink - because you are up and running in no time, think jsFiddle jsBin codepen plnkr for node apps. On this occasion Rubber Duck method proved itself useful (as always) so no silly questions posted to StackOverflow.