There is a pretty good Fetch API polyfill for Node.js: https://www.npmjs.com/package/isomorphic-fetch. As a simple example I want to fetch an image within Node.js:

I define a “toBlob” module because in Node.js environment the response object have no blob() function. I’ll keep in mind that body data are received chunk per chunk and you can listen on ‘data’ and ‘end’ events.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
export default response => {
    let buffer;
    let chunks = [];
 
    response.body.on('data', chunk => {
 
        chunks.push(chunk);
    });
 
    return new Promise((resolve, reject) => (
        response.body.on('end', () => {
 
            buffer = Buffer.concat(chunks);
            resolve({
                contentType: response.headers.get('Content-Type'),
                data: buffer
            })
        })
    ));
 
};

btw. node streams can also be an option.

usage Fetch API:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import fetch from 'isomorphic-fetch';
import toBlob from './fetch-blob';
 
fetch('http://example.com/image.png', {method: 'GET'})
    .then(toBlob)
    .then(image => {
        console.log(image);
        res.contentType(image.contentType);
        res.send(image.data);
    })
    .catch(err => {
        console.log(err.message);
        res.end();
    });