javascriptcorslocalhoststrapi

Setting cors in strapi 4


I'm using strapi 4 as localhost and got a problem with cors settings when I add new assets via url

Image upload takes CORS error Access to XMLHttpRequest at 'https://www.countrysideveterinaryclinic.org/sites/default/files/interesting-cat-facts.jpg' from origin 'http://localhost:1337' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Solution from here didn't help

my middleware.js

module.exports = [
  'strapi::errors',
  'strapi::security',
  'strapi::poweredBy',
  {
    name: 'strapi::cors',
    config: {
      enabled: true,
      header: '*',
      origin: ['http://localhost:1337']
    }
  },
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::session',
  'strapi::favicon',
  'strapi::public',
];

Solution

  • I found the solution myself, maybe it will help someone

    middlewares.js

    module.exports = [
      'strapi::errors',
      'strapi::security',
      'strapi::poweredBy', // here
      {
        name: 'strapi::cors',
        config: {
          enabled: true, // deprecated in v4.25.8
          headers: '*',
          origin: ['http://localhost:1337', 'http://example2']
        }
      },
      'strapi::logger',
      'strapi::query',
      'strapi::body',
      'strapi::session',
      'strapi::favicon',
      'strapi::public',
    ];