Restify and Angular CORS No 'Access-Control-Allow-Origin' header is present on the requested resource

cors, javascript, restify

Solution

The problem was faced because of restify has internal CORS module who manage CORS logic. in this module you could find list of allowed headers, by default it's

[
        'accept',
        'accept-version',
        'content-type',
        'request-id',
        'origin',
        'x-api-version',
        'x-request-id'
]

As I say in the question, I use bearer token auth, so I send my request with `Authorization` header. It's not included in default list, and that's why my request fails.

To fix that problem we need to add this header to the list of ALLOW_HEADERS. for that in my restify configuration code I add this line:

restify.CORS.ALLOW_HEADERS.push('authorization');

Think that info could be helpfull if you faced with similar problem, because I spend a lot to find the solution.

Problem

I faced with that problem when implementing REST api with Restify secured with bearer token authorization type. when I sending simple get request to API server it fails with CORS problem 405 (Method Not Allowed) angular.js:7962 OPTIONS http://api.host.com/tests No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://local.host.com' is therefore not allowed access. Solution described in my answer, so it's not real question for me, because I placed it when already know the answer, but hope it will save time for someone else in future.

Original source