Using node-fetch in Node.js

The node-fetch library brings the Fetch API to Node.js, allowing you to make HTTP requests in a way that’s similar to the browser’s fetch function. This is particularly useful for server-side applications that need to interact with APIs or fetch data from external sources. Note that when making HTTPS requests, you may need to handle SSL certificates appropriately.

Installation

First, install node-fetch via npm:

npm install node-fetch

Then, require it in your code:

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

Fetching Text or HTML

To fetch plain text or HTML content from a URL, use the text() method on the response.

fetch('https://github.com/')
  .then(res => res.text())
  .then(body => console.log(body))
  .catch(err => console.error('Error fetching text:', err));

Explanation: This example fetches the HTML content of GitHub’s homepage and logs it to the console. Always handle errors with .catch() to manage network issues or invalid responses.

Fetching JSON

For JSON APIs, use the json() method to parse the response body as JSON.

fetch('https://api.github.com/users/github')
  .then(res => res.json())
  .then(json => console.log(json))
  .catch(err => console.error('Error fetching JSON:', err));

Explanation: This fetches user data from the GitHub API and parses it as JSON. The json() method returns a promise that resolves to the parsed object.

Handling HTTPS Requests

When dealing with HTTPS URLs, especially self-signed certificates, you may need to configure an agent to bypass certificate validation. Use Node.js’s https module for this.

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

const agent = new https.Agent({
  rejectUnauthorized: false  // Only use in development; not recommended for production
});

fetch('https://example.com/api/data', { agent })
  .then(res => res.json())
  .then(data => console.log(data))
  .catch(err => console.error('Error fetching HTTPS:', err));

Warning: Setting rejectUnauthorized: false disables SSL verification, which can expose your application to security risks. In production, use valid certificates or configure the agent properly.

Using Async/Await

For cleaner, synchronous-looking code, use async/await with node-fetch.

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

async function fetchData() {
  try {
    const res = await fetch('https://api.github.com/users/github');
    if (!res.ok) {
      throw new Error(`HTTP error! status: ${res.status}`);
    }
    const json = await res.json();
    console.log(json);
  } catch (err) {
    console.error('Error:', err);
  }
}

fetchData();

Explanation: This async function waits for the fetch and JSON parsing, with proper error handling for HTTP status codes.

Best Practices

  • Error Handling: Always check res.ok or handle status codes explicitly.
  • Timeouts: Set timeouts to avoid hanging requests.
  • Headers: Use the headers option to set custom headers, like authorization.
  • Security: Avoid disabling SSL verification in production. Use environment variables for sensitive configurations.
  • Performance: Reuse agents for multiple requests to the same host.

Resources