The node middle layer requests the back-end service. Why can't await get the data from the request-promise-native module?

use node as the middle layer request server, please spray gently. The project is as follows:

  1. node version 8.11.2, using koa2 version 2.5.1
  2. because the node server is needed to request the backend api, the request module is used
  3. uses the koa2-cors package (2.0.5) for cross-domain processing
  4. uses koa-router (7.1.1)
// login
const request = require("request")
router.post("/login", async (ctx, next) => {
  ctx.body = {  // 
    code: 200,
    msg: "success"
   }  
})
// 
router.post("/login", async (ctx, next) => {
    // 
    
   // api 
    request({
    url: "http://xx.55.41.71:9090/tokens",
    method: "POST",
    json: true,
    headers: {
      "content-type": "application/json",
    },
    body: JSON.stringify({
      "userName": "xxxxxx",
      "password": "xxxxxx"
    })
  }, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log(body)  // 
      ctx.body = body  //  request-promise-native
    }
  })
    
})

for more information about the above, please refer to the link

.

problem description

//requestrequest-promise-native
// const request = require("request")  // request  
// request-promise-nativenpminstall requese --save 
// demorequire("requese") npm install requese --save 
 const request = require("request-promise-native")
// 
let res = await request({
    url: "http://120.55.41.71:9090/tokens",
    method: "POST",
    headers: {
      "content-type": "application/json",
    },
    body: JSON.stringify({
      "userName": ctx.request.body.name,
      "password": ctx.request.body.password
    })
  })
  ctx.body = res

node reports an error as follows

because the time is relatively late, I am not sure that the error in the backend has caused my error (because I just found a problem with the postman call but I am not sure), so I feel that my node has made a mistake.
I want a big god to say what the mistake is? It is best to give a demo example of request-promise-native, look at the official documents of github, and have little knowledge of it in English. Look at the official request-promise (it seems to need the support of another plug-in), request-promise-native (native implementation, can not be used) request-promise-any (it seems to need a support, too). Which of these works well?


just encapsulate it yourself based on promise, and you don't have to rely on third-party packages for everything.

router.post('/login', async (ctx, next) => {
  const options = {
    url: 'http://xx.55.41.71:9090/tokens',
    method: "POST",
    json: true,
    headers: {
      "content-type": "application/json",
    },
    body: JSON.stringify({
      'userName': 'xxxxxx',
      'password': 'xxxxxx'
    })
  };
  ctx.body = await requestPromise(options);
});

// promise 
function requestPromise (options) {  
  return new Promise((resolve, reject) => {
    request(options, (error, response, body) => {
      if (error) {
        reject(error);
      }
      resolve(body);
    })
  })
}

or, more simply, directly use the native promisify:

const util = require('util');
const request = require('request');
const requestPromise = util.promisify(request);

router.post('/login', async (ctx, next) => {
  const options = {
    url: 'http://xx.55.41.71:9090/tokens',
    method: "POST",
    json: true,
    headers: {
      "content-type": "application/json",
    },
    body: JSON.stringify({
      'userName': 'xxxxxx',
      'password': 'xxxxxx'
    })
  };

  const {response, body} = await requestPromise(options);
  ctx.body = body;
});
Menu