egg-axios
v1.1.6
Published
egg axios plugin
Downloads
486
Maintainers
Readme
egg-axios
axios plugin for Egg.js.
NOTE: This plugin just for integrate axios into Egg.js, more documentation please visit https://github.com/axios/axios.
Install
$ npm i --save egg-axios
Usage & configuration
config.default.js
exports.http = {
headers: {
common: {
'Content-Type': 'application/json; charset=UTF-8'
}
},
timeout: 10000
};
config/plugin.js
exports.http = {
enable: true,
package: 'egg-axios'
}
example
// controller.js or service.js
// with promise
this.ctx.http.get('/user', {id: 123}).then((data)=>{ // ==> /user?id=123
// data is only remote server response data
console.log(data);
}).catch((err)=>{
console.error(err);
});
this.ctx.http.get('/user/:id', {id: 123}).then((data)=>{ // ==> /user/123
// data is only remote server response data
console.log(data);
}).catch((err)=>{
console.error(err);
});
this.ctx.http.post('/post', {postId: 123}).then((data)=>{
// data is only remote server response data
console.log(data);
}).catch((err)=>{
console.error(err);
});
// with await/async
try {
const data = await this.ctx.http.get('/user', {id: 123});
console.log(data);
} catch (e) {
console.error(e)
}
try {
const data = await this.ctx.http.post('/post', {postId: 123});
console.log(data);
} catch (e) {
console.error(e)
}
more example please visit https://github.com/axios/axios.