Fetching Remote Data within your Node Application

Last Updated On Sunday 19th Dec 2021

Request a remote server.

  • In This Article, We are going to use the node-fetch.
  • It is one of the popular and widely used modules.
  • node-fetch brings us the browser’s fetch to Node.

Lets Intstal with npm

	npm i node-fetch

After Installing Now, Can use it as simply as below.

	const fetch = require('node-fetch');

fetch('https://example.com').then( res => res.text()).then(body => // Do Domehing With it)
  • It provides an API that the browser’s window.fetch, allowing our Node program to access remote resources.
  • Like window.fetch, it offers support for the HTTP methods of GET, POST, DELETE, and PUT.
  .then(res => res.json())
  .then(json => console.log(json));
  • We Can use the async/await syntax, including a try/catch block for error handling
	(async () => {
  try {
    const response = await fetch('https://example.com/some');
    const json = await response.json();
  } catch (error) {
  • node-fetch can also handle POST, DELETE, and PUT methods, allowing you to send data to a server.
	const data = {
  name: "Adam",
  job: "Web Developer"
  • Now We make a POST request
	fetch('https://example.com', {
    method: 'post',
    body:    JSON.stringify(data),
    headers: { 'Content-Type': 'application/json' },
  .then(res => res.json())
  .then(json => console.log(json));


  • UseFul Library for fetching Remote Dtatas node-fetch