Moiz Sohail
Moiz Sohail

Reputation: 648

How to setup nuxtjs proxy to avoid cors when connecting with strapi

I am trying to connect to strapi js on localhost:1337 but I am getting cors error on my post request. I researched a lot over it and found some tutorials over how to setup nuxtjs/proxy to avoid this cors issue.

The following is my relevant proxy tag in the nuxt.config.js

proxy: {
  '/*': {
    target: '[::1]:8080/',
    changeOrigin: true,
    pathRewrite: { '^/*': '' },
  },
},

And the relevant axios tag

axios: { proxy: true },

Upvotes: 2

Views: 5599

Answers (1)

Moiz Sohail
Moiz Sohail

Reputation: 648

Update: The following solution has been tested on nuxt2.

Found the solution

Just copy paste the following in nuxt.config.js

proxy: {
  '/api': {
    target: 'http://localhost:1337',
    changeOrigin: true,
    pathRewrite: { '^/api': '/' },
  },
}

And append all the server related requests with /api

For example my nuxtjs/auth code now becomes

auth: {
  strategies: {
    local: {
      endpoints: {
        login: {
          url: '/api/auth/local', //From /auth/local
          method: 'post',
          propertyName: 'jwt',
        user: { url: '/api/session' //From /session
        ...

Upvotes: 3

Related Questions