You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can pass options using module options or `axios` section in `nuxt.config.js`
147
+
148
+
You can pass options using module options or `axios` section in `nuxt.config.js`
149
+
150
+
### `prefix`, `host` and `port`
151
+
152
+
This options are used for default values of `baseURL` and `browserBaseURL`.
153
+
154
+
Can be customized with `API_PREFIX`, `API_HOST` (or `HOST`) and `API_PORT` (or `PORT`) environment variables too.
155
+
156
+
Default value of `prefix` is `/`.
143
157
144
158
### `baseURL`
145
-
- Default: `http://[HOST]:[PORT]/api`
146
159
147
-
Base URL is required for requests in server-side & SSR and prepended to all requests with relative path.
148
-
You can also use environment variable `API_URL` which **overrides**`baseURL`.
160
+
* Default: `http://[HOST]:[PORT][PREFIX]`
161
+
162
+
Base URL is required for requests in server-side & SSR and prepended to all axios requests.
163
+
164
+
Environment variable `API_URL` can be used to **override**`baseURL`.
149
165
150
166
### `browserBaseURL`
151
-
- Default: `/api`
152
167
153
-
Base URL which is used in client side prepended to all requests with relative path.
154
-
You can also use environment variable `API_URL_BROWSER` which **overrides**`browserBaseURL`.
168
+
* Default: `baseURL` (or `prefix` when `options.proxyMode` is `true`)
169
+
170
+
Base URL which is used in client side and prepended to all axios requests.
155
171
156
-
- If `browserBaseURL` is not provided it defaults to `baseURL` value.
157
-
- If hostname & port of `browserbaseURL` are equal to nuxt server, it defaults to relative part of `baseURL`.
158
-
So if your nuxt application is being accessed under a different domain, requests go to same origin and prevents Cross-Origin problems.
172
+
Environment variable `API_URL_BROWSER` can be used to **override**`browserBaseURL`.
159
173
160
174
### `credentials`
161
-
- Default: `true`
175
+
176
+
* Default: `true`
162
177
163
178
Adds an interceptor to automatically set `withCredentials` config of axios when requesting to `baseUrl`
164
179
which allows passing authentication headers to backend.
165
180
166
181
### `debug`
167
-
- Default: `false`
182
+
183
+
* Default: `false`
168
184
169
185
Adds interceptors to log all responses and requests
170
186
171
187
### `proxyHeaders`
172
-
- Default: `true`
188
+
189
+
* Default: `true`
173
190
174
191
In SSR context, sets client request header as axios default request headers.
175
192
This is useful for making requests which need cookie based auth on server side.
@@ -178,15 +195,18 @@ Also helps making consistent requests in both SSR and Client Side code.
178
195
> **NOTE:** If directing requests at a url protected by CloudFlare's CDN you should set this to false to prevent CloudFlare from mistakenly detecting a reverse proxy loop and returning a 403 error.
179
196
180
197
### `proxyHeadersIgnore`
181
-
- Default `['host', 'accept']`
198
+
199
+
* Default `['host', 'accept']`
182
200
183
201
Only efficient when `proxyHeaders` is set to true. Removes unwanted request headers to the API backend in SSR.
184
202
185
203
### `redirectError`
186
-
- Default: `{}`
204
+
205
+
* Default: `{}`
187
206
188
207
This option is a map from specific error codes to page which they should be redirect.
189
208
For example if you want redirecting all `401` errors to `/login` use:
209
+
190
210
```js
191
211
axios: {
192
212
redirectError: {
@@ -196,7 +216,8 @@ axios: {
196
216
```
197
217
198
218
### `requestInterceptor`
199
-
- Default: `null`
219
+
220
+
* Default: `null`
200
221
201
222
Function for manipulating axios requests. Useful for setting custom headers,
202
223
for example based on the store state. The second argument is the nuxt context.
Now you can make requests to backend: (Works fine in both SSR and Browser)
385
+
351
386
```js
352
387
asyncasyncData({ app }) {
353
388
// Magically makes request to http://www.mocky.io/v2/59388bb4120000dc00a672e2
@@ -360,13 +395,15 @@ async asyncData({ app }) {
360
395
```
361
396
362
397
Details
363
-
-`'@nuxtjs/axios'`
364
-
- By default axios plugin sets base url to `http://[host]:[port]/api` which is `http://localhost:3000/api`
365
398
366
-
-`'/api': 'http://www.mocky.io/v2'`
367
-
- This line creates a server middleware to pass requests from `/api` to `http://www.mocky.io/v2`
368
-
- We used `pathRewrite` to remove `/api` from starting of requests and change it to `/v2`
369
-
- For more information and advanced usage please refer to [proxy](https://github.com/nuxt-community/modules/blob/master/packages/proxy) docs.
399
+
*`'@nuxtjs/axios'`
400
+
401
+
* By default axios plugin sets base url to `http://[host]:[port]/api` which is `http://localhost:3000/api`
402
+
403
+
*`'/api': 'http://www.mocky.io/v2'`
404
+
* This line creates a server middleware to pass requests from `/api` to `http://www.mocky.io/v2`
405
+
* We used `pathRewrite` to remove `/api` from starting of requests and change it to `/v2`
406
+
* For more information and advanced usage please refer to [proxy](https://github.com/nuxt-community/modules/blob/master/packages/proxy) docs.
0 commit comments