I want to create a API proxy that is Node based but I also want to add CORS at the same time. The console doesn’t allow CORS to be added at time of creation. Also when I try to add CORS policy to an existing proxy it’s doesn’t show up in the policy drop-down menu. Any suggestions on the best way to handle either of the 2 issues above?
Solved! Go to Solution.
I tried your suggestion and that didn't work either, I think the reason is it couldn't find CORS.
So, I downloaded the proxy from the console, then I manually added "node_modules/cors" to the resource folder and I added CORS and CORSPreflight to the policies folder. Zipped it and uploaded a new version of the proxy using the console.
This may be overkill, but it's now working on both my mobile app and Javascript/HTML.
Thanks for your help.
I encourage you to try CORS NPM module. You can enable it by trying this:
var express = require('express') , cors = require('cors') , app = express(); app.get('/products/:id', cors(), function(req, res, next){ res.json({msg: 'This is CORS-enabled for all origins!'}); }); app.listen(80, function(){ console.log('CORS-enabled web server listening on port 80'); });
More info available on NPM.
I’m having the hardest time getting pass this CORS issue when using Javascript/HTML
vdex-baas/postNewUser. (Reason: CORS header 'Access-Control-Allow-Origin' missing). <unknown> Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource...
Don’t know if I’m using your cors suggestion property.
Here is my server-usergrid.js code, what am I doing wrong?
var argo = require('argo');
var express = require('express') , cors = require('cors') , app = express();
var usergrid = require('usergrid');
var RemoteController = require('./RemoteController');
app.use(express.logger('dev'));
app.use(express.bodyParser())
app.use(app.router);app.get('/products/:id', cors(), function(req, res, next){
res.json({msg: 'This is CORS-enabled for all origins!'});
});
var proxy = argo() .target('https://api.usergrid.com') .build();
app.get('/hello', function(req, res) { res.setHeader("Access-Control-Allow-Origin","*"); res.send('Hello from Express vDex-BaaS version 2'); });
app.post('/getAllUsers', function(req, res) { res.setHeader("Access-Control-Allow-Origin","*"); RemoteController.getAllUsers(req, res); });
app.all('*', proxy.run); app.listen(80);
Try enabling CORS for all resources:
var express = require('express') , cors = require('cors') , app = express(); app.use(cors()); app.get('/products/:id', function(req, res, next){ res.json({msg: 'This is CORS-enabled for all origins!'}); }); app.listen(80, function(){ console.log('CORS-enabled web server listening on port 80'); });
I tried your suggestion and that didn't work either, I think the reason is it couldn't find CORS.
So, I downloaded the proxy from the console, then I manually added "node_modules/cors" to the resource folder and I added CORS and CORSPreflight to the policies folder. Zipped it and uploaded a new version of the proxy using the console.
This may be overkill, but it's now working on both my mobile app and Javascript/HTML.
Thanks for your help.
Good catch! I forgot to mention. Most NPM modules need to be uploaded along with Apigee artifacts (XML descriptor files) or call NPM Install management API.
It’s been a while since I dealt with this, but I think the answer I gave (see best answer) is how I solved the issue. Download the proxy using the console then add everything you need either to a folder, in the case of the actual CORS files, then I think I found a sample API Proxy example (maybe as one of the examples Apigee gives you) that contained CORS and CORSPreflight info with the correct info. Repackaged everything then I uploaded using the console. I don’t think I can help any further as I too found the console not as helpful with the issue as you would expect for such a common problem.
I'm sure there are better ways of handling CORS issues, once I got this to work I stopped looking, maybe someone else will chime in.
User | Count |
---|---|
5 | |
2 | |
2 | |
1 | |
1 |