通过 fetch 请求一个api,并进行打印,node 引入fetch需要引入node-fetch 包

    1. const fetch = require("node-fetch")
    2. function getZhihuColumn(id) {
    3. const url = `https://zhuanlan.zhihu.com/api/columns/${id}`
    4. fetch(url)
    5. .then(res => res.json())
    6. .then(column => {
    7. console.log(`标题:${column.title}`)
    8. console.log(`简介:${column.intro}`)
    9. })
    10. }
    11. getZhihuColumn("feweekly")

    通过async/await将代码拆分的扁平一些:

    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. const column = await response.json()
    6. console.log(`标题:${column.title}`)
    7. console.log(`简介:${column.intro}`)
    8. }
    9. getZhihuColumn("feweekly")