2
votes

NodeJS - Angularjs

CORS Issue.

I am having an issue with REST POST to nodeJS. REST GET seems to work fine.

I am seeing the typical message and I can't seem to fix it.

XMLHttpRequest cannot load http://test.dev:3000/api/profile. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. The response had HTTP status code 400.

NodeJS -- I have the following set in my app.js

var cors = require('cors')

app.use(cors());
// Enables CORS

var enableCORS = function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

  // intercept OPTIONS method
  if ('OPTIONS' == req.method) {
   // res.send(200);
    res.sendStatus(200);
  }
  else {
    next();
  }
};

// enable CORS!
app.use(enableCORS);
//--------------

server log: OPTIONS /api/profile 204 1.257 ms - - POST /api/profile 400 65.141 ms - 698

NodeJS -- routes

// get email sent - Sept 12, 2015
router.post('/profile', function (req, res, next) {
    console.log('api: -- start profile update -- auth server ');
    console.log('api: -- parms --->> ',  req.params);
    console.log('api: -- body --->> ',  req.body);
    console.log('api: -- body.test --->> ',  req.body.test);
    //var updatedColumns = req.body.updatedColumns;
    ...
    res.status(200).jsonp({ok: 'ok' });

});

Angularjs side..

service

angular.module('testApp').factory('ProfileUpdateService',  ['$resource', function($resource) {
  return $resource('http://test.dev:3000/api/profile');
}]);


controller
      ProfileUpdateService.save('test data', function(data){
        console.log('called save on ProfileUpdateService');
      });
1
Why are you both using the cors module and hand-implementing your own CORS support? Shouldn't you be doing one or the other? I'm guessing that those two implementations may be messing each other up. In particular your own support for OPTIONS may be messing up with the CORS module you're using. - jfriend00
I have take one out then tested. Removed other tested. Same issue. - philipfwilson
I am going thru Nginx do you think thats an issue? - philipfwilson

1 Answers

0
votes

Fixed. It looks like I had several cors filters in use.