C
C
cester2017-11-08 19:53:39
JavaScript
cester, 2017-11-08 19:53:39

How to make a response result of module execution, nodejs/express?

Good afternoon! Please tell me why I can not get to the response? (nodejs / express) I'm trying
to write a module that would receive data from another server ... it receives data, it is displayed in the console, but how to make the response be the result of the execution of this module, so that I can send them further to the client?
It doesn't work out how to do it.
Thank you!
The module itself

var request = require('request');
var rp = require('request-promise');

loadData = () => {
  var options = {
    uri: 'http://fake-domen.com/...',
    headers: {
      'User-Agent': 'Request-Promise'
    },
    json: true // Automatically parses the JSON string in the response
  };
  rp(options)
  .then(function (repos) {
    console.log(repos, 'repos');
    return repos
  })
  .catch(function (err) {
    // API call failed...
  });
}

exports.func = loadData;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Taratin, 2017-11-08
@Taraflex

No way. Neither request nor request-promise can make requests that block an event loop.

C
Coder321, 2017-11-09
@Coder321

loadData = () => {
  var options = {
    uri: 'http://fake-domen.com/...',
    headers: {
      'User-Agent': 'Request-Promise'
    },
    json: true // Automatically parses the JSON string in the response
  };
  return rp(options)
  .then(function (repos) {
    console.log(repos, 'repos');
    return repos
  })
  .catch(function (err) {
    // API call failed...
  });
}

const func = require('./func);
func()
.then()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question