Brendan
Brendan

Reputation: 132

How to create automatic relative base URL in Nuxt Axios API calls (for unknown deployment domains)

Can't seem to find this anywhere...

Is it possible for Axios calls within Nuxt to be called with automatic relative paths?

To state the case more specifically, I'd like to be able to not have to set a server-side API (express serverMiddleware) base URL specifically, but make it update for:

  1. localhost:3000
  2. myliveurl.com
  3. generated-netlify-url-with-93394932-numbers.netlify.com (the kicker here -- for deploy previews.)

Would appreciate any help.

Thanks!

Upvotes: 1

Views: 1746

Answers (1)

Andrey Shatilov
Andrey Shatilov

Reputation: 576

Just add this to your nuxt.config.js

axios: {
    baseURL: '/api/',
},

and this will use relative directory api/:

  1. localhost:3000/api/...
  2. myliveurl.com/api/...
  3. generated-netlify-url-with-93394932-numbers.netlify.com/api/...

Upvotes: 3

Related Questions