NodeJS(一)——基础 API

1. API URL

在node中输入命令行url:
这里写图片描述

可以看到url总共有5个function方法。

  1. url.parse()
    将一个url地址转换成一个url对象
    输入命令行:url.parse(‘http://my.csdn.net/my/mycsdn‘)
    这里写图片描述

  2. url.format()
    将一个解析后的URL对象、转成、一个格式化的URL字符串。
    输入命令行:url.format({
    protocol: ‘http:’,
    slashes: true,
    auth: null,
    host: ‘www.imooc.com:8080’,
    port: ‘8080’,
    hostname: ‘www.imooc.com’,
    hash: ‘#floor1’,
    search: ‘?from=Allen&course=node’,
    query: ‘from=Allen&course=node’,
    pathname: ‘/video/6710’,
    path: ‘/video/6710?from=Allen&course=node’,
    href: ‘http://www.imooc.com:8080/video/6710?from=Allen&course=node#floor1
    })
    这里写图片描述

  3. url.resolve(from,to)
    为URL或 href 插入 或 替换原有的标签。

2.API querystring

  1. querystring.stringify()
    把一个对象序列化
    输入命令行:
querystring.stringify({
    name:'Allen',course:['jade','node'],from:''
 })

输出结果:

name:'Allen',course:['jade','node'],from:''

API中有三个参数:

> querystring.stringify({name:'Allen',course:['jade','node'],from:''})
'name=Allen&course=jade&course=node&from='
> querystring.stringify({name:'Allen',course:['jade','node'],from:''},',')
'name=Allen,course=jade,course=node,from='
> querystring.stringify({name:'Allen',course:['jade','node'],from:''},',',':')
'name:Allen,course:jade,course:node,from:'

2.querystring.parse()
反序列化

> querystring.parse('name=Allen&course=jade&course=node&from=')
{ name: 'Allen', course: [ 'jade', 'node' ], from: '' }
> querystring.parse('name=Allen,course=jade,course=node,from=',',')
{ name: 'Allen', course: [ 'jade', 'node' ], from: '' }
> querystring.parse('name:Allen,course:jade,course:node,from:',',',':')
{ name: 'Allen', course: [ 'jade', 'node' ], from: '' } 

3.转义
querystring.escape()

> querystring.escape('你好世界')
'%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C'

4.反转义
querystring.unescape()

 > querystring.unescape('%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C')
'你好世界'
在Node.js中使用axios请求接口以及拦截器的使用方法如下: 首先,需要安装axios模块: ``` npm install axios ``` 然后,在代码中引入axios模块: ```js const axios = require('axios'); ``` 接下来,可以使用axios发送GET、POST等请求: ```js axios.get('http://example.com/api') .then(response => { console.log(response.data); }) .catch(error => { console.log(error); }); axios.post('http://example.com/api', {data: 'hello'}) .then(response => { console.log(response.data); }) .catch(error => { console.log(error); }); ``` 当然,axios还支持一些其他的请求方法,比如PUT、DELETE等。 接下来,我们可以使用axios的拦截器对请求和响应进行处理。例如,我们可以在请求头中添加token: ```js axios.interceptors.request.use(config => { const token = localStorage.getItem('token'); if (token) { config.headers.Authorization = `Bearer ${token}`; } return config; }, error => { return Promise.reject(error); }); ``` 在上面的代码中,我们使用了axios的interceptors.request.use方法,它可以在请求被发送出去之前对其进行拦截处理。在这个例子中,我们将localStorage中存储的token添加到请求头中的Authorization字段中。 我们还可以使用axios的interceptors.response.use方法对响应进行处理。例如,我们可以检查响应状态码是否为401,如果是,则跳转到登录页面: ```js axios.interceptors.response.use(response => { return response; }, error => { if (error.response.status === 401) { window.location.href = '/login'; } return Promise.reject(error); }); ``` 在上面的代码中,我们使用了axios的interceptors.response.use方法,它可以在响应被接收之前对其进行拦截处理。在这个例子中,我们检查了响应状态码是否为401,并且在这种情况下跳转到登录页面。 以上就是在Node.js中使用axios请求接口及拦截器的使用方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值