title: NodeJS + Express with Angular on Localhost: Blocked by CORS preflight
body: <p>I have exhausted trying all the solutions given in these posts:</p>
  
  <p><a href="https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe">No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource—when trying to get data from a REST API</a></p>
  
  <p><a href="https://stackoverflow.com/questions/29547003/angularjs-no-access-control-allow-origin-header-is-present-on-the-requested-r">AngularJS: No &quot;Access-Control-Allow-Origin&quot; header is present on the requested resource</a></p>
  
  <p><a href="https://stackoverflow.com/questions/43150051/how-to-enable-cors-nodejs-with-express">How to enable cors nodejs with express?</a></p>
  
  <p>I have tried using cors, which seemed to be the popular solution but I am still getting the following error:</p>
  
  <pre><code>Access to XMLHttpRequest at 'http://localhost:3000/email' from origin 'http://localhost:63342' has been blocked by CORS policy: Method GET' is not allowed by Access-Control-Allow-Methods in preflight response.
  </code></pre>
  
  <p>This is my server side (NodeJS) setup:
  <strong>Attempt #1</strong>(without cors module)</p>
  
  <pre><code>var createError = require('http-errors');
  var express = require('express');
  var path = require('path');
  var cookieParser = require('cookie-parser');
  var logger = require('morgan');
  
  var indexRouter = require('./routes/index');
  var usersRouter = require('./routes/users');
  var cors = require('cors')
  var app = express();
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'pug');
  
  app.use(logger('dev'));
  app.use(express.json());
  app.use(express.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  
  function setupCORS(req, res, next) {
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-type,Accept,X-Access-Token,X-Key');
    res.header('Access-Control-Allow-Origin', '*');
    if (req.method === 'OPTIONS') {
      res.status(200).end();
    } else {
      next();
    }
  }
  app.all('/*', setupCORS);
  
  
  app.use('/', indexRouter);
  app.use('/users', usersRouter);
  </code></pre>
  
  <p><strong>Attempt #2:</strong> (with 'cors' module)</p>
  
  <pre><code>var createError = require('http-errors');
  var express = require('express');
  var path = require('path');
  var cookieParser = require('cookie-parser');
  var logger = require('morgan');
  
  var indexRouter = require('./routes/index');
  var usersRouter = require('./routes/users');
  var cors = require('cors')
  var app = express();
  app.use(cors());
  
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'pug');
  
  app.use(logger('dev'));
  app.use(express.json());
  app.use(express.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  
  app.use('/', indexRouter);
  app.use('/users', usersRouter);
  </code></pre>
  
  <p>None of these worked and I don't how to fix this.
  I am running the server localhost and this is how I make the GET call in my Angular:</p>
  
  <pre><code>$scope.getAllCustomerEmails = function() {
          $scope.textBox= "getAllCustomerEmails";
  
          $http({
              method: "GET'",
              url: "http://localhost:3000/email"
          }).then(function successCallback(response) {
              //console.log("/email response: ", response);
              $scope.textBox = response;
  
          }, function errorCallback(response) {
              $scope.textBox = "GET Error";
          });
      };
  </code></pre>
  
  <p>Any help is much appreciated.</p>