Created
May 29, 2015 10:08
-
-
Save grabcode/ea73716f4883ee705139 to your computer and use it in GitHub Desktop.
Iron Router CORS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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