node封装博客园接口

目的:将博客园提供的api 封装成我们可以想要的json数据类型

依赖库:axios xml2js

直接上代码:

import fetch from '../utils/fetch'
import * as xml2js from 'xml2js'
const parser = new xml2js.Parser({ explicitArray: false, ignoreAttrs: true })

class Blog {
  async getBlogList(ctx) {
    try {
      const { page, limit } = ctx.request.query
      const xml = await fetch({ url: `http://wcf.open.cnblogs.com/blog/u/lyxverycool/posts/${page}/${limit}` })
      let blogLists;
      parser.parseString(xml, (err, res) => {
        blogLists = res.feed
      })
      ctx.body = {
        status: '1',
        type: 'success_query_blogList_all',
        message: 'success',
        data: blogLists
      }
    } catch (err) {
      ctx.body = {
        status: '0',
        type: 'error_query_blogList_all',
        message: err.toString()
      }
    }
  }
  async getBlogDetail(ctx) {
    try {
      const { id } = ctx.request.query
      const blogDetail = await fetch({ url: `http://wcf.open.cnblogs.com/blog/post/body/${id}` })
      ctx.body = {
        status: '1',
        type: 'success_query_blogDetail',
        message: 'success',
        data: blogDetail
      }
    } catch (err) {
      ctx.body = {
        status: '0',
        type: 'error_query_blogDetail',
        message: err.toString()
      }
    }
  }
}

export default new Blog()

  

转载于:https://www.cnblogs.com/lyxverycool/articles/10861264.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值