Skip to content

Instantly share code, notes, and snippets.

@grabcode
Created May 29, 2015 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grabcode/ea73716f4883ee705139 to your computer and use it in GitHub Desktop.
Save grabcode/ea73716f4883ee705139 to your computer and use it in GitHub Desktop.
Iron Router CORS
/*
Enable iron-router route to run cross-origin resource sharing
Usage:
Router.route('/ping', function(){
(new Cors(this)).respondWith({"oh": "yeah"});
}, {where: 'server'});
Note: dependency on underscore.js (what meteorjs app doesn't anyway ;)
*/
var Cors = function(route){
_.extend(this, route);
this.response.statusCode = 200;
this.response.setHeader("Content-Type", "application/json");
this.response.setHeader("Access-Control-Allow-Origin", "*");
this.response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
};
Cors.prototype.respondWith = function(payload){
this.response.end( this.params.query.callback+'('+JSON.stringify(payload)+')' );
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment