-
-
Save tomysmile/20005231cb8801a90c56 to your computer and use it in GitHub Desktop.
Node API server with CORS disabled, AngularJS controllers with proxy and no proxy, and the ionic.project settings to allow proxy
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() | |
app.get('/api/feed', function(req, res) { | |
res.json({name: 'feed', items: ['first', 'second']}) | |
}) | |
var server = app.listen(3000, function () { | |
var host = server.address().address | |
var port = server.address().port | |
console.log('Example app listening at http://%s:%s', host, port) | |
}) |
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
//Say we ran `ionic serve` | |
//This will access proxy, and pass along feed correctly | |
angular.module('starter.controllers', []) | |
.controller('DashCtrl', function($scope, $http) { | |
$http.get('/api/feed').then(function(data) { | |
console.log('data ' , data) | |
}) | |
}) | |
//This will not access proxy, instead sending request straight across origin | |
//causing a failure to happen. | |
// XMLHttpRequest cannot load http://0.0.0.0:3000/api/feed. | |
//No 'Access-Control-Allow-Origin' header is present on the requested resource. | |
//Origin 'http://localhost:8100' is therefore not allowed access. | |
.controller('FeedCtrl', function($scope, $http) { | |
$http.get('http://0.0.0.0:3000/api/feed').then(function(data) { | |
console.log('data ' , data) | |
}) | |
}) |
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": "1test", | |
"app_id": "", | |
"proxies": [ | |
{ | |
"path": "/api", | |
"options": "http://0.0.0.0:3000/api" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment