Introduction
Fetch has finally been added to Nodejs version v17.5.0 as an experimental feature. Fetch is the defacto way of making HTTP requests on the browser after the XMLHttpRequest dark days 💀
Fetch in Nodejs
Fetch was added to Nodejs v17.5.0
as an experimental feature and later fully supported in the v18.0.0
release.
Example usage in code snippet:
try {
const response = await fetch(`https://jsonplaceholder.typicode.com/todos/1'`)
const todos = await response.json()
console.log(todos)
} catch(err) {
console.err(`Failed to fetch:`, ${err})
}
Always wrap
async-awaitinside a try-catch block to catch exceptions and handle errors better.
View code example on Github