Using Node.js to simulate HTTP requests is common, but because Node modules (native and third-party libraries) provide methods that are asynchronous, it is difficult to use in many scenarios, rather than synchronous. Here is a summary of the ways in which several common library apis can go from asynchronous to synchronous. The modules include: request, request-promise, request-promise-native, request-promise-any

PS: Node version >=8.0.0 to use Async/Await PS: The auth field is added for requests from applications that require a username and password, such as RabbitMQ. Pages that do not require login can remove this parameter.

The first kind of

Use the native module Util with its Promisify API as shown in the following code example:

const request = require('request'); const util = require('util'); var url = "https://www.baidu.com/"; const getPromise = util.promisify(request.get); // PS: The auth field is used for requests from applications that require a username and password, such as RabbitMQ. Then ((value)=>{console.log("value", value); //1: no auth argument getPromise(url). }).catch((err)=>{ console.log("err" , err ); }); //2: native with auth parameter getPromise(url, {'auth' : {' user' : 'xx', 'pass' : 'xx', 'sendImmediately' : 'false', }}).then((value)=>{ console.log("value" , value ); }).catch((err)=>{ console.log("err" , err ); }); // Async /await // this is the most recommended, using only util and request. async function handle(){ let result = await getPromise(url , {'auth' : { 'user' : 'xx', 'pass' : 'xx', 'sendImmediately' : 'false', }}); // Add.catch() console.log("result", result.); } handle(); [1], 'request.get().auth()' cannot be used in asynchronous variable synchronization.Copy the code

The second,

Use the request-promise-native module. The request-promise-native module is written using native promise. Check the source code and you can see that it is inherited from the Request module.

// Stop writing native examplesthenConst RPN = require(const RPN = require();'request-promise-native');  
var url = "https://www.baidu.com/";
async function useRequestPromiseNative(){// Select * from interface CoreOptions (); // Select * from interface CoreOptions ();let options = {
        method: 'GET',
        uri: url,
        auth : {
            'user' : 'xx'.'pass' : 'xx'.'sendImmediately' : 'false',}};let  rpnbody = await rpn(options);       
    
    console.log("rpnbody", rpnbody ); } useRequestPromiseNative(); // post example const RPN = require('request-promise-native');
var url = "https://www.baidu.com/";
async function useRequestPromiseNative() {let options = {
        method: 'POST', uri: url, body: {// define your body here} json:true, // This depends on your parameters};let  rpnbody = await rpn(options);       
    
    console.log("rpnbody" , rpnbody );
}
useRequestPromiseNative();
Copy the code

The third kind of

Use the module request-Promise, request-Promise is written based on Bluebird, check the source code can see inherited from the Request module, code examples are as follows:

Const rp = require(const rp = require('request-promise');
var url = "https://www.baidu.com/";
async function useRequestPromise() {let options = {
        method: 'GET', uri: url, auth: {// Can be removed'user' : 'xx'.'pass' : 'xx'.'sendImmediately' : 'false',}};let  rpbody = await rp(options);       
    console.log("rpnbody" , rpbody );
}

useRequestPromise();
Copy the code

A fourth

Use the request-promise-any module, request-promise-any is also written based on request, the following code example:

Const rpa = require(const rpa = require('request-promise-any');
var url = "https://www.baidu.com/";
async function useRequestPromiseAny() {let options = {
        method: 'GET',
        uri: url,
        auth : {
            'user' : 'xx'.'pass' : 'xx'.'sendImmediately' : 'false',}};let  rpabody = await rpa(options);       
    console.log("rpabody" , rpabody );
}

useRequestPromiseAny();
Copy the code

The fifth

Use bluebird to convert promisifyAll API into Promise, as shown in the following code:

const Promise = require('bluebird');
const request = require('request');
var url = "https://www.baidu.com/";
Promise.promisifyAll(request, { suffix: 'SC'}); //suffix custom get --> getSC asyncfunction usebluebird() {let result = await request.getSC(url , {'auth' : {
        'user' : 'xx'.'pass' : 'xxx'.'sendImmediately' : 'false',}}); console.log("result" , result);
}

usebluebird()
Copy the code

There are 5 ways to use it. There are more than 5 ways to use it. Choose according to your needs.