如果接口返回的不是 200,那么将会抛出错误信息

    1. const fetch = require("node-fetch")
    2. async function getZhihuColumn(id) {
    3. const url = `https://zhuanlan.zhihu.com/api/columns/${id}`
    4. const response = await fetch(url)
    5. if(response.status !== 200) {
    6. throw new Error(response.statusText)
    7. }
    8. return await response.json()
    9. }
    10. // 添加处理函数
    11. const showCloumnInfo = async (id) => {
    12. try {
    13. const column = await getZhihuColumn(id)
    14. console.log(`标题:${column.title}`)
    15. console.log(`简介:${column.intro}`)
    16. } catch (e) {
    17. console.error(e)
    18. }
    19. };
    20. showCloumnInfo("feweekly123")