Configuring CORS headers

Please be sure of your agent type and version and pick the right documentation accordingly.

This is the documentation of the forest-express-sequelize and forest-express-mongoose Node.js agents that will soon reach end-of-support.

forest-express-sequelize v9 and forest-express-mongoose v9 are replaced by @forestadmin/agent v1.

Please check your agent type and version and read on or switch to the right documentation.

Configuring CORS headers

Depending on how you've setup your app, you may encounter a CORS error. It will look like this in your browser console:

In this case, you need to configure the right CORS headers to allow the domain to trigger an API call on your Application URL, which is a different domain name (e.g. localhost:3000 on development).

We use the Rack CORS Gem for this purpose.

# Gemfile
source ''

# ...

gem 'forest_liana'
gem 'rack-cors'
module YourApp
  class Application < Rails::Application
    # ...

    # For Rails 5, use the class Rack::Cors. For Rails 4, you MUST use the string 'Rack::Cors'.
    null_regex =\Anull\z/)
    config.middleware.insert_before 0, Rack::Cors do
      allow do
        hostnames = [null_regex, 'localhost:4200', '', 'localhost:3001']
        hostnames += ENV['CORS_ORIGINS'].split(',') if ENV['CORS_ORIGINS']
        origins hostnames
        resource '*',
          headers: :any,
          methods: :any,
          expose: ['Content-Disposition'],
          credentials: true

Last updated