https://github.com/zhangmingfeng/egg-axios
egg axios plugin
https://github.com/zhangmingfeng/egg-axios
axios egg egg-axios egg-plugin nodejs
Last synced: 6 months ago
JSON representation
egg axios plugin
- Host: GitHub
- URL: https://github.com/zhangmingfeng/egg-axios
- Owner: zhangmingfeng
- License: mit
- Created: 2018-04-17T13:57:14.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-12-04T02:09:21.000Z (over 4 years ago)
- Last Synced: 2024-11-22T03:06:22.342Z (7 months ago)
- Topics: axios, egg, egg-axios, egg-plugin, nodejs
- Language: JavaScript
- Size: 19.5 KB
- Stars: 19
- Watchers: 5
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# egg-axios
[axios](https://github.com/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
```bash
$ npm i --save egg-axios
```## Usage & configuration
- `config.default.js`
```js
exports.http = {
headers: {
common: {
'Content-Type': 'application/json; charset=UTF-8'
}
},
timeout: 10000
};
```- `config/plugin.js`
``` js
exports.http = {
enable: true,
package: 'egg-axios'
}
```### example
```js
// 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);
});
```
```js
this.ctx.http.post('/post', {postId: 123}).then((data)=>{
// data is only remote server response data
console.log(data);
}).catch((err)=>{
console.error(err);
});
```
```js
// with await/async
try {
const data = await this.ctx.http.get('/user', {id: 123});
console.log(data);
} catch (e) {
console.error(e)
}
```
```js
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.