From the front end and through jQuery I am making GET requests to a node/express app. This app then speaks to the spotify API to authenticate a user. I am getting the following error message when I try and make a call to the presave endpoint.
:8000/#access_token=AccessTokenProvidedBySpotify XMLHttpRequest cannot load http://XXX.XX.XXX.XXX/presave. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://XXX.XX.XXX.XXX:8000' is therefore not allowed access.
I have tried adding the following code to my app.js express app, but it didn't work. I tried everything else i could find online too, but cannot get it working.
var app = express();
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});
app.get('/presave', function(req, res){
    var options = {
        url: 'https://api.spotify.com/v1/me',
        headers: {
            'Authorization': 'Bearer ' + access_token
        },
        json: true
    };
    requestPromise(options).then(function(body) {
        userEmail = body.email;
        userId = body.id;
        var optionsTwo = {
            url: 'https://api.spotify.com/v1/users/' + userId + '/playlists',
            headers: {
                'Authorization': 'Bearer ' + access_token
            },
            json: true
        };
        return rp(optionsTwo).then(function (body) {
            playlists = body.items;
            res.json(playlists);
        });
    }).catch(function (err) {
        console.log(err)
    })
});
EDIT
I have also implemented the answer provided below, but this doesn't work either. If I console.log(req.method) it is always GET, it never shows OPTIONS.
var app = express();
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  if (req.method === 'OPTIONS') {
    return res.send(200);
  } else {
    return next();
  }
});
My front end code looks like this:
$.ajax({
    type: 'GET',
    url: 'http://xxx.xx.xxx.xxx/presave',
    contentType: 'application/json',
    success: function(data) {
        console.log(data)
    }
});
