简体   繁体   中英

return result from controller to route node js

I wanted to get the result from controller to route then I will render is to view using NodeJS.

var request = require('request');
var crypto = require('crypto');

username = "hello@.com.ph";
password = 123124123;

auth = "Basic " + Buffer.from(username + ":" + password).toString("base64");

exports.textname = function(req, res, next) {
  request.post({
    url : 'https://urlsample/api/getlist',
    headers : {
        "Authorization" : auth
    }
  }, function (error, response, body) {
    return res.json(body);
  });
};

In the code above, the anonymous function you pass on to request.post as the second parameter is a callback, and as such what you return from it is discarded. To return the body of the response, you should call next() after res.json(body) :

// code from above...

exports.textname = function(req, res, next) {
  request.post({
    url : 'https://urlsample/api/getlist',
    headers : {
        "Authorization" : auth
    }
  }, function (error, response, body) {
    res.json(body);
    next();
  });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM