Node.js axios库基本用法
<p>axios是基于Promise的HTTP客户端,可以在浏览器和Node.js中使用。</p><p>GitHub地址:https://github.com/axios/axios</p>
<p><strong>安装</strong></p>
<div class="cnblogs_code">
<pre>npm install axios</pre>
</div>
<p><strong>新建一个服务器并启动,做为数据请求响应的例子。</strong></p>
<div class="cnblogs_code">
<pre>const http = require('http'<span style="color: rgba(0, 0, 0, 1)">);
const url </span>= require("url"<span style="color: rgba(0, 0, 0, 1)">);
const util </span>= require('util'<span style="color: rgba(0, 0, 0, 1)">);
const querystring </span>= require('querystring'<span style="color: rgba(0, 0, 0, 1)">);
const port </span>= 3000<span style="color: rgba(0, 0, 0, 1)">;
http.createServer((req, res) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
res.statusCode </span>= 200<span style="color: rgba(0, 0, 0, 1)">,
res.setHeader(</span>'Content-Type', 'text/plain;charset=utf-8'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(req.method === 'GET'<span style="color: rgba(0, 0, 0, 1)">) {
toGet(req, res);
}</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(req.method === 'POST'<span style="color: rgba(0, 0, 0, 1)">) {
toPost(req, res);
}
}).listen(port, () </span>=><span style="color: rgba(0, 0, 0, 1)"> {
console.log(`Server listening on: http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">localhost:${port}`);</span>
<span style="color: rgba(0, 0, 0, 1)">});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取GET请求内容 </span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> toGet(req, res){
let data </span>= 'GET请求内容:\n' +<span style="color: rgba(0, 0, 0, 1)"> util.inspect(url.parse(req.url));
res.end(data);
console.log(data);
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取POST请求内容、cookie </span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> toPost(req, res){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义了一个data变量,用于暂存请求体的信息</span>
let data = ''<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中</span>
req.on('data', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(chunk){
data </span>+=<span style="color: rgba(0, 0, 0, 1)"> chunk;
});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 在end事件触发后,通过querystring.parse将post解析为真正的POST请求格式,然后向客户端返回。</span>
req.on('end', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">data = querystring.parse(data);</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">res.end('POST请求内容:\n' + util.inspect(data));</span>
data = 'POST请求内容:\n' +<span style="color: rgba(0, 0, 0, 1)"> data;
res.end(data);
console.log(data);
console.log(</span>'cookie内容:\n' +<span style="color: rgba(0, 0, 0, 1)"> req.headers.cookie);
});
}</span></pre>
</div>
<p><span style="font-size: 15px"><strong>客户端axios的请求用法</strong></span></p>
<p><strong>GET请求用法1,不带参数。</strong></p>
<div class="cnblogs_code">
<pre>const axios = require('axios'<span style="color: rgba(0, 0, 0, 1)">);
axios.get(</span>'http://localhost:3000'<span style="color: rgba(0, 0, 0, 1)">)
.then(res </span>=><span style="color: rgba(0, 0, 0, 1)"> {
console.log(res.data);
})
.</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(error =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(error);
});</span></pre>
</div>
<p>运行结果</p>
<div class="cnblogs_code">
<pre>GET请求内容:
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: '/',
path: '/',
href: '/'
}</pre>
</div>
<p> </p>
<p><strong>GET请求用法2,带参数。</strong></p>
<div class="cnblogs_code">
<pre>const axios = require('axios'<span style="color: rgba(0, 0, 0, 1)">);
axios.get(</span>'http://localhost:3000'<span style="color: rgba(0, 0, 0, 1)">,{
params:{
id: </span>123<span style="color: rgba(0, 0, 0, 1)">,
name: </span>'aa'<span style="color: rgba(0, 0, 0, 1)">
}
})
.then(res </span>=><span style="color: rgba(0, 0, 0, 1)"> {
console.log(res.data);
})
.</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(error =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(error);
});</span></pre>
</div>
<p>运行结果</p>
<div class="cnblogs_code">
<pre>GET请求内容:
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?id=123&name=aa',
query: 'id=123&name=aa',
pathname: '/',
path: '/?id=123&name=aa',
href: '/?id=123&name=aa'
}</pre>
</div>
<p> </p>
<p><strong>GET请求用法3,带参数</strong>。运行结果同上。</p>
<div class="cnblogs_code">
<pre>const axios = require('axios'<span style="color: rgba(0, 0, 0, 1)">);
axios({
method: </span>'get'<span style="color: rgba(0, 0, 0, 1)">,
url: </span>'http://localhost:3000'<span style="color: rgba(0, 0, 0, 1)">,
params:{
id: </span>123<span style="color: rgba(0, 0, 0, 1)">,
name: </span>'aa'<span style="color: rgba(0, 0, 0, 1)">
}
})
.then(res </span>=><span style="color: rgba(0, 0, 0, 1)"> {
console.log(res.data);
}).</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(error =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(error);
});</span></pre>
</div>
<p> </p>
<p><strong>POST请求用法1。</strong></p>
<div class="cnblogs_code">
<pre>const axios = require('axios'<span style="color: rgba(0, 0, 0, 1)">);
axios.post(</span>'http://localhost:3000'<span style="color: rgba(0, 0, 0, 1)">,{
id: </span>123<span style="color: rgba(0, 0, 0, 1)">,
name: </span>'aa'<span style="color: rgba(0, 0, 0, 1)">
})
.then(res </span>=><span style="color: rgba(0, 0, 0, 1)"> {
console.log(res.data);
})
.</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(error =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(error);
});</span></pre>
</div>
<p> </p>
<p><strong>POST请求用法2。</strong>运行结果同上。</p>
<div class="cnblogs_code">
<pre>const axios = require('axios'<span style="color: rgba(0, 0, 0, 1)">);
axios({
method: </span>'post'<span style="color: rgba(0, 0, 0, 1)">,
url: </span>'http://localhost:3000'<span style="color: rgba(0, 0, 0, 1)">,
data: {
id: </span>'123'<span style="color: rgba(0, 0, 0, 1)">,
name: </span>'aa'<span style="color: rgba(0, 0, 0, 1)">
}})
.then(res </span>=><span style="color: rgba(0, 0, 0, 1)"> {
console.log(res.data);
})
.</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(error =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(error);
});</span></pre>
</div>
<p> </p>
<p><strong>设置默认请求的前缀地址</strong></p>
<div class="cnblogs_code">
<pre>const axios = require('axios'<span style="color: rgba(0, 0, 0, 1)">);
axios.defaults.baseURL </span>= 'http://localhost:3000'<span style="color: rgba(0, 0, 0, 1)">;
axios.get(</span>'/test?id=1'<span style="color: rgba(0, 0, 0, 1)">)
.then(res </span>=><span style="color: rgba(0, 0, 0, 1)"> {
console.log(res.data);
})
.</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(error =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(error);
});</span></pre>
</div>
<p> </p>
<p><strong>设置请求头信息</strong></p>
<div class="cnblogs_code">
<pre>const axios = require('axios'<span style="color: rgba(0, 0, 0, 1)">);
axios({
method: </span>'post'<span style="color: rgba(0, 0, 0, 1)">,
url: </span>'http://localhost:3000'<span style="color: rgba(0, 0, 0, 1)">,
data: {
id: </span>'123'<span style="color: rgba(0, 0, 0, 1)">,
name: </span>'aa'<span style="color: rgba(0, 0, 0, 1)">
},
headers:{
</span>'Content-Type': 'application/json'<span style="color: rgba(0, 0, 0, 1)">,
</span>'Cookie': 'id=123;name=aa;'<span style="color: rgba(0, 0, 0, 1)">
}})
.then(res </span>=><span style="color: rgba(0, 0, 0, 1)"> {
console.log(res.data);
})
.</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(error =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(error);
});</span></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/gdjlc/p/14568817.html
頁:
[1]