axios-module icon indicating copy to clipboard operation
axios-module copied to clipboard

setting baseUrl base on req.headers.host or req.headers['x-forwarded-host']

Open yakovyarmo opened this issue 4 years ago • 1 comments

Hi, I'm running NUXT in SSR mode with multiple domains. What I'm trying to achieve is to set the base url based on the request headers in order for the backend to return the right response. Another solution that I thought about it to send the request with x-forwarded-host header to the backend but again I don't know how to set it dynamically to all requests based on the headers.

Any help would be appreciated

yakovyarmo avatar Nov 10 '20 08:11 yakovyarmo

Hey, Sorry for the late response.
You can create a custom plugin and and modify axios headers.

// ~/plugins/some-plugin.js
export default function (ctx, inject) {
  if (process.server) {
    // use `ctx.req` to extract host
  } else {
    // plugin runs on client side, use `window.location` to extract host
  }
}

Checkout module docs to see how to change baseURL and more

farnabaz avatar Feb 03 '21 15:02 farnabaz