Node.js 有没有办法让Axios将数据作为默认响应返回?
当我们使用Axios时,我们总是需要从响应中获取数据。像这样:Node.js 有没有办法让Axios将数据作为默认响应返回?,node.js,express,axios,Node.js,Express,Axios,当我们使用Axios时,我们总是需要从响应中获取数据。像这样: const response = await Axios.get('/url') const data = response.data const data = await Axios.get('/url') 有没有办法让Axios返回数据?像这样: const response = await Axios.get('/url') const data = response.data const data = await Ax
const response = await Axios.get('/url')
const data = response.data
const data = await Axios.get('/url')
有没有办法让Axios返回数据?像这样:
const response = await Axios.get('/url')
const data = response.data
const data = await Axios.get('/url')
除了来自响应的数据之外,我们从未使用过任何东西。添加响应拦截器
axios.interceptors.response.use(function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
return response.data; // do like this
}, function (error) {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
return Promise.reject(error);
});
我通常会创建一个名为interceptors.js的js文件
import axios from 'axios';
export function registerInterceptors() {
axios.interceptors.response.use(
function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
return response.data;
},
function (error) {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
return Promise.reject(error);
}
);
}
import { registerInterceptors } from './path/to/interceptors';
registerInterceptors();//this will register the interceptors.
const execute = ({url, method, params, data}) => {
return axios({
url,
method,//GET or POST
data,
params,
});
}
const get = (url, params) => {
return execute({
url, method: 'GET', params
})
}
const post = (url, data) => {
return execute({
url, method: 'POST', data
})
}
export default {
get,
post,
};
在/src/index.js
import axios from 'axios';
export function registerInterceptors() {
axios.interceptors.response.use(
function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
return response.data;
},
function (error) {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
return Promise.reject(error);
}
);
}
import { registerInterceptors } from './path/to/interceptors';
registerInterceptors();//this will register the interceptors.
const execute = ({url, method, params, data}) => {
return axios({
url,
method,//GET or POST
data,
params,
});
}
const get = (url, params) => {
return execute({
url, method: 'GET', params
})
}
const post = (url, data) => {
return execute({
url, method: 'POST', data
})
}
export default {
get,
post,
};
为了获得最佳实践,不要在任何地方都使用axios
,以防万一,如果您想迁移到不同的http
提供商,那么您必须更改它使用的任何地方
围绕axios创建一个包装器,并在应用程序中使用该包装器
例如:
创建一个名为http.js的js文件
import axios from 'axios';
export function registerInterceptors() {
axios.interceptors.response.use(
function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
return response.data;
},
function (error) {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
return Promise.reject(error);
}
);
}
import { registerInterceptors } from './path/to/interceptors';
registerInterceptors();//this will register the interceptors.
const execute = ({url, method, params, data}) => {
return axios({
url,
method,//GET or POST
data,
params,
});
}
const get = (url, params) => {
return execute({
url, method: 'GET', params
})
}
const post = (url, data) => {
return execute({
url, method: 'POST', data
})
}
export default {
get,
post,
};
像这样使用它
import http from './http';
....
http.get('url', {a:1, b:2})
因此,现在您可以在整个应用程序中进行自定义,即使更改http提供程序也非常简单。添加响应拦截器
axios.interceptors.response.use(function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
return response.data; // do like this
}, function (error) {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
return Promise.reject(error);
});
我通常会创建一个名为interceptors.js的js文件
import axios from 'axios';
export function registerInterceptors() {
axios.interceptors.response.use(
function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
return response.data;
},
function (error) {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
return Promise.reject(error);
}
);
}
import { registerInterceptors } from './path/to/interceptors';
registerInterceptors();//this will register the interceptors.
const execute = ({url, method, params, data}) => {
return axios({
url,
method,//GET or POST
data,
params,
});
}
const get = (url, params) => {
return execute({
url, method: 'GET', params
})
}
const post = (url, data) => {
return execute({
url, method: 'POST', data
})
}
export default {
get,
post,
};
在/src/index.js
import axios from 'axios';
export function registerInterceptors() {
axios.interceptors.response.use(
function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
return response.data;
},
function (error) {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
return Promise.reject(error);
}
);
}
import { registerInterceptors } from './path/to/interceptors';
registerInterceptors();//this will register the interceptors.
const execute = ({url, method, params, data}) => {
return axios({
url,
method,//GET or POST
data,
params,
});
}
const get = (url, params) => {
return execute({
url, method: 'GET', params
})
}
const post = (url, data) => {
return execute({
url, method: 'POST', data
})
}
export default {
get,
post,
};
为了获得最佳实践,不要在任何地方都使用axios
,以防万一,如果您想迁移到不同的http
提供商,那么您必须更改它使用的任何地方
围绕axios创建一个包装器,并在应用程序中使用该包装器
例如:
创建一个名为http.js的js文件
import axios from 'axios';
export function registerInterceptors() {
axios.interceptors.response.use(
function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
return response.data;
},
function (error) {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
return Promise.reject(error);
}
);
}
import { registerInterceptors } from './path/to/interceptors';
registerInterceptors();//this will register the interceptors.
const execute = ({url, method, params, data}) => {
return axios({
url,
method,//GET or POST
data,
params,
});
}
const get = (url, params) => {
return execute({
url, method: 'GET', params
})
}
const post = (url, data) => {
return execute({
url, method: 'POST', data
})
}
export default {
get,
post,
};
像这样使用它
import http from './http';
....
http.get('url', {a:1, b:2})
因此,现在您可以在整个应用程序中进行自定义,即使更改http提供程序也非常简单。您可以像这样使用ES6销毁:
const { data } = await Axios.get('/url');
因此,您不必再编写一行代码。您可以像这样使用ES6销毁:
const { data } = await Axios.get('/url');
因此,您不必再编写另一行代码。“const{data}=await Axios.etc”可能会起作用。“const{data}=await Axios.etc”可能会起作用。只需确保此拦截器在需要实际响应的任何内容之后。回答得好!给我们两个选择。我喜欢包腋窝的尖端。这样我们就不需要拦截器了,我们可以很容易地定制所有的请求。只要确保这个拦截器是针对任何需要实际响应的请求。回答得很好!给我们两个选择。我喜欢包腋窝的尖端。这样我们就不需要拦截器,我们可以轻松地定制所有请求。