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.

2 comments:

  1. Im no expert, but I believe you just made an excellent You certainly understand what youre speaking about, and I can truly get behind that.
    Regards,
    Node JS training in chennai

    ReplyDelete
  2. Cross origin resource sharing is very important invention. Thank you for these code examples, I think I can use them for my projects and couponessaygroup.com promotion. Hope that they are useful not only for me. I prefer programs that could be used by big groups of people.

    ReplyDelete