ChildOfCode


Code, Maker, Robotic, Open Source. Knowledge Bases


Replacing the deprecated request module

Request module is deprecated and not longer maintenance and update by Mikeal Rogers.

I have been using Request module very long times. Almost every NodeJS developer have use using this popular module.

request is also the first package on npm package and is the also first module use on my NodeJS production module.

The main reason Request module is deprecated because the request module code structure is older many latest javascript feature is not supported For example, async/await feature and the main reason is the vulnerability threats will cause many security .

Update the whole new structure of module will take a longer times and on npm have many other module can replace request module so the module author is not longer maintenance the module.

First let take a long how the request module working.

request module

var request = require('request');

function requestModule()  
{
    request('https://nodejs.org/en/', function (error, response, body) {
      console.log('statusCode:', response && response.statusCode);
      console.log('error:', error );
      console.log('body:', body );
    });
}

requestModule();

Let try the most popular HTTP module axios

axios module

const axios = require('axios');

function axiosModule()  
{
  axios.get('https://nodejs.org/en/')
  .then(function (response) {
    // handle success
    console.log(response.status);
    console.log(response.data);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
    console.log("Done");
  }); 
}

axiosModule(); 

Also let have a look the Human-friendly and powerful HTTP request library for Node.js

got module

const got = require('got');

function gotModule()  
{
  (async () => {
      try {
          const response = await got('https://nodejs.org/en/');
          console.log(response.body);
          console.log(response.statusCode)
      } catch (error) {
          console.log(error.response.body);
      }
  })();
}

gotModule();

After some test axios Module is more suitable for my current project.
axios module not only available on NodeJS also available on frontend Browser support.
On new project maybe will try to use got module regarding got module have more feature support compare with axios.