Skip to main content
added 27 characters in body
Source Link
Mike 'Pomax' Kamermans
  • 54.3k
  • 17
  • 134
  • 182

Node v18 and on come with native Fetch API support built right into Node itself. No need for third party libraries, imports, requires, or small hand-crafted shims, just use global fetch the way you're used to from the browser.

const fs = require(`fs`); // As per the note above: remove the following line for Node 18 and above const fetch = require(`./that-code-shown-above.js`); fetch(`https://placekittenplacecats.com/20000/300`) .then(res => res.arrayBuffer()) .then(bytes => fs.writeFileSync(`kitten.jpg`, new Uint8Array(bytes))) .catch(e => console.error(e)); try { const response = await fetch(`https://jsonplaceholder.typicode.com/todos/1`); const data = await response.json(); console.log(data); } catch (e) { console.error(e); } // etc. 

Node v18 and on come with native Fetch API support built right into Node itself. No need for third party libraries or small hand-crafted shims, just use fetch the way you're used to from the browser.

const fs = require(`fs`); // As per the note above: remove the following line for Node 18 and above const fetch = require(`./that-code-shown-above.js`); fetch(`https://placekitten.com/200/300`) .then(res => res.arrayBuffer()) .then(bytes => fs.writeFileSync(`kitten.jpg`, new Uint8Array(bytes))) .catch(e => console.error(e)); try { const response = await fetch(`https://jsonplaceholder.typicode.com/todos/1`); const data = await response.json(); console.log(data); } catch (e) { console.error(e); } // etc. 

Node v18 and on come with native Fetch API support built right into Node itself. No need for third party libraries, imports, requires, or small hand-crafted shims, just use global fetch the way you're used to from the browser.

const fs = require(`fs`); // As per the note above: remove the following line for Node 18 and above const fetch = require(`./that-code-shown-above.js`); fetch(`https://placecats.com/00/300`) .then(res => res.arrayBuffer()) .then(bytes => fs.writeFileSync(`kitten.jpg`, new Uint8Array(bytes))) .catch(e => console.error(e)); try { const response = await fetch(`https://jsonplaceholder.typicode.com/todos/1`); const data = await response.json(); console.log(data); } catch (e) { console.error(e); } // etc. 
added 16 characters in body
Source Link
Mike 'Pomax' Kamermans
  • 54.3k
  • 17
  • 134
  • 182
const fs = require(`fs`); // As per the note above: remove the following line for Node 18 and above const fetch = require(`./that-code-shown-above.js`); fetch(`https://placekitten.com/200/300`) .then(res => res.arrayBuffer()) .then(bytes => fs.writeFileSync(`kitten.jpg`, new Uint8Array(bytes))) .catch(e => console.error(e)); try { const response = await fetch(`https://jsonplaceholder.typicode.com/todos/1`); const data = await response.json(); console.log(data); } catch (e) { console.error(e); } // etc. 
const fs = require(`fs`); // As per the note above: remove the following line for Node 18 and above const fetch = require(`./that-code-shown-above.js`); fetch(`https://placekitten.com/200/300`) .then(res => res.arrayBuffer()) .then(bytes => fs.writeFileSync(`kitten.jpg`, bytes)) .catch(e => console.error(e)); try { const response = await fetch(`https://jsonplaceholder.typicode.com/todos/1`); const data = await response.json(); console.log(data); } catch (e) { console.error(e); } // etc. 
const fs = require(`fs`); // As per the note above: remove the following line for Node 18 and above const fetch = require(`./that-code-shown-above.js`); fetch(`https://placekitten.com/200/300`) .then(res => res.arrayBuffer()) .then(bytes => fs.writeFileSync(`kitten.jpg`, new Uint8Array(bytes))) .catch(e => console.error(e)); try { const response = await fetch(`https://jsonplaceholder.typicode.com/todos/1`); const data = await response.json(); console.log(data); } catch (e) { console.error(e); } // etc. 
added 77 characters in body
Source Link
Mike 'Pomax' Kamermans
  • 54.3k
  • 17
  • 134
  • 182
const fs = require(`fs`);   // As per the note above: remove the following line for Node 18 and above const fetch = require(`./that-code-shown-above.js`); fetch(`https://placekitten.com/200/300`) .then(res => res.arrayBuffer()) .then(bytes => fs.writeFileSync(`kitten.jpg`, bytes)) .catch(e => console.error(e)); try { const response = await fetch(`https://jsonplaceholder.typicode.com/todos/1`); const data = await response.json(); console.log(data); } catch (e) { console.error(e); } // etc. 
const fs = require(`fs`); const fetch = require(`./that-code-shown-above.js`); fetch(`https://placekitten.com/200/300`) .then(res => res.arrayBuffer()) .then(bytes => fs.writeFileSync(`kitten.jpg`, bytes)) .catch(e => console.error(e)); try { const response = await fetch(`https://jsonplaceholder.typicode.com/todos/1`); const data = await response.json(); console.log(data); } catch (e) { console.error(e); } // etc. 
const fs = require(`fs`);   // As per the note above: remove the following line for Node 18 and above const fetch = require(`./that-code-shown-above.js`); fetch(`https://placekitten.com/200/300`) .then(res => res.arrayBuffer()) .then(bytes => fs.writeFileSync(`kitten.jpg`, bytes)) .catch(e => console.error(e)); try { const response = await fetch(`https://jsonplaceholder.typicode.com/todos/1`); const data = await response.json(); console.log(data); } catch (e) { console.error(e); } // etc. 
added 40 characters in body
Source Link
Mike 'Pomax' Kamermans
  • 54.3k
  • 17
  • 134
  • 182
Loading
added 111 characters in body
Source Link
Mike 'Pomax' Kamermans
  • 54.3k
  • 17
  • 134
  • 182
Loading
added 10 characters in body
Source Link
Mike 'Pomax' Kamermans
  • 54.3k
  • 17
  • 134
  • 182
Loading
edited body
Source Link
Mike 'Pomax' Kamermans
  • 54.3k
  • 17
  • 134
  • 182
Loading
added 49 characters in body
Source Link
Mike 'Pomax' Kamermans
  • 54.3k
  • 17
  • 134
  • 182
Loading
added 141 characters in body
Source Link
Mike 'Pomax' Kamermans
  • 54.3k
  • 17
  • 134
  • 182
Loading
added 2 characters in body
Source Link
Mike 'Pomax' Kamermans
  • 54.3k
  • 17
  • 134
  • 182
Loading
added 2 characters in body
Source Link
Mike 'Pomax' Kamermans
  • 54.3k
  • 17
  • 134
  • 182
Loading
Source Link
Mike 'Pomax' Kamermans
  • 54.3k
  • 17
  • 134
  • 182
Loading