Getting started with node-fetch




In this post, we will see how to use node-fetch

What is node-fetch

node-fetch is a library used to send requests server-side.

Installation

Run the below command,

npm install node-fetch

or

yarn add node-fetch

GET Requests

Plain text or HTML

fetch('http://localhost:5000/')
    .then(res => res.text())
    .then(body => console.log(body));
    

JSON

fetch('http://localhost:5000')
    .then(res => res.json())
    .then(json => console.log(json));

POST Requests

Simple Post

fetch('http://localhost:5000/post', { method: 'POST', body: 'a=1' })
    .then(res => res.json())
    .then(json => console.log(json));