Skip to content

Instantly share code, notes, and snippets.

@tomysmile
Forked from jbavari/app.js
Created October 19, 2015 07:21
Show Gist options
  • Save tomysmile/20005231cb8801a90c56 to your computer and use it in GitHub Desktop.
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
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)
})
//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)
})
})
{
"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