Reputation: 95
I've tried a couple of things, and they both work on my localhost (headers are present and correct), but not on the live server , which is amazon hosted, built from a bitnami NodeJS image behind an apache reverse proxy.
Configurations I've tried:
1) Set response header manually for each route necessary (so this code was in a routing file):
router.options('/:var1/:var2', function(req, res) {
res.setHeader("Access-control-allow-origin", "*");
var db = req.db;
var data = db.get('data');
actions.find({"criteria":req.params.var1}, function(err, docs)
{
res.send(docs);
});
});
2) Use middleware from NPM (this code is in app.js):
//Includes/Requirements
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongodb = require('mongodb');
var monk = require('monk');
var cors = require('cors');
var db = monk('localhost:27017/app');
...
app.use(cors());
So using both of these techniques requests to localhost:3000 result in the expected headers (Access-allow-control-origin:*). However, when I push the code to my live server, those headers are not present in the responses.
Any ideas?
Upvotes: 3
Views: 2232
Reputation: 695
I have solved it using cors module .
var cors = require('cors');
app.use(cors()); // add this line at appropriate place in your code
then add app.options('*', cors()); before all the route is defined or included in server.js/app.js file
Upvotes: 2