13

I was looking for modern modules that implement basic HTTP methods such as GET, POST in Node.js.

I guess the most popular is request. The async/await version of it is called request-promise-native.

Recently I learned that these modules are being deprecated. So, what modern alternatives can I use that are built on the async/await paradigm?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Sergey Avdeev
  • 910
  • 1
  • 8
  • 16

4 Answers4

8

I'd strongly suggest using node-fetch. It is based on the fetch API in modern browsers. Not only is it promise-based it also has an actual standard behind it.

The only reason you wouldn't use fetch is if you don't like the API. Then I'd suggest using something cross-platform like axios or superagent.

I personally find using the same API on the server and browser eases maintainability and offers potential for code reuse.

slebetman
  • 109,858
  • 19
  • 140
  • 171
5

Just for having another option in mind, I would suggest using the node native http module.

import * as http from 'http';

async function requestPromise(path: string) {
    return new Promise((resolve, reject) => {
        http.get(path, (resp) => {
            let data = '';

            resp.on('data', (chunk) => {
                data += chunk;
            });

            resp.on('end', () => {
                resolve(data);
            });

        }).on("error", (error) => {
            reject(error);
        });
    });
}

(async function () {
    try {
        const result = await requestPromise('http://www.google.com');
        console.log(result);
    } catch (error) {
        console.error(error);
    }
})();

costadvl
  • 128
  • 2
  • 12
2

On the same github issue for request there is another link which talks about the alternatives. You can see them here. It clearly explains different types and wht style they are (promise/callback).

Ashish Modi
  • 7,529
  • 2
  • 20
  • 35
1

Got is pretty sweet.

"Got was created because the popular request package is bloated. Furthermore, Got is fully written in TypeScript and actively maintained." - https://www.npmjs.com/package/got#faq

enter image description here

Yarin
  • 173,523
  • 149
  • 402
  • 512