Last active
December 2, 2015 16:32
-
-
Save stefek99/a336b44cff783c20dd70 to your computer and use it in GitHub Desktop.
Setting CORS (cross origin resource sharing) headers in node.js express app
This file contains hidden or 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
| var express = require('express'); | |
| var app = express(); | |
| var fs = require('fs'); | |
| var cors = require('cors'); | |
| 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()); | |
| }); | |
| app.get('/index.html', function(req, res) { | |
| res.send("whatever sending nothing"); | |
| }); | |
| app.get('/getblah', function(req, res) { | |
| fs.readFile("static/blah.json", function (err, data) { | |
| if (err) { | |
| console.log(err); | |
| console.log("Cannot get static/blah.json"); | |
| res.send("Cannot get static/blah.json"); | |
| return; | |
| } | |
| res.send(data); | |
| }); | |
| }); | |
| app.listen(3000); | |
| console.log("node app started at port 3000"); |
This file contains hidden or 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
| {"something" : "whatever", "_comment" : "should be put in -- static -- directory"} |
This file contains hidden or 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
| { | |
| "name": "cors-demo", | |
| "description": "cors demo", | |
| "version": "0.0.1", | |
| "dependencies": { | |
| "express": "3.x", | |
| "cors": "*" | |
| } | |
| } |
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script> | |
| <meta charset="utf-8"> | |
| <title>JS Bin</title> | |
| <script> | |
| $(function() { | |
| // I was testing againts nitrousbox --> probably the quickest way to get up and running (in no time) | |
| $.ajax({ | |
| url: "http://gray-warrior-nodejs-65-103631.euw1-2.nitrousbox.com/getblah", | |
| success: function(data){ | |
| console.log(data); | |
| }, | |
| error: function(jqXHR, textStatus, errorThrown) { | |
| console.log(jqXHR, textStatus, errorThrown); | |
| }, | |
| dataType: "json" | |
| }); | |
| $.ajax({ | |
| url: "http://gray-warrior-nodejs-65-103631.euw1-2.nitrousbox.com/blah.json", | |
| success: function(data){ | |
| console.log(data); | |
| }, | |
| error: function(jqXHR, textStatus, errorThrown) { | |
| console.log(jqXHR, textStatus, errorThrown); | |
| }, | |
| dataType: "json" | |
| }); | |
| // $.get("http://localhost:3000/blah.json", function(data){ | |
| // console.log(data); | |
| // }) | |
| // $.get("http://localhost:3000/getblah", function(data){ | |
| // console.log(data); | |
| // }) | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment