node-fetch usage

本文简单示范,如何在后端Node.js中使用fetch,特别是请求https时候需要注意。

Fetch Examples

Fetch text/html

fetch('https://github.com/')
    .then(res => res.text())
    .then(body => console.log(body));

Fetch json

 
fetch('https://api.github.com/users/github')
    .then(res => res.json())
    .then(json => console.log(json));

Fetch HTTPS

const https = require("https");
const agent = new https.Agent({
  rejectUnauthorized: false
})
fetch(myUrl, { agent })

Resources

https://www.npmjs.com/package/node-fetch https://github.com/bitinn/node-fetch/issues/19

相关文章: