V
V
Vadim V2018-04-22 15:37:17
Node.js
Vadim V, 2018-04-22 15:37:17

How can I do it right in the request module?

Who is familiar with the request module please tell me.

const pathAPI = '/api/blog';
  const requestOptions = {
    url: apiOptions.server + pathAPI,
    method: 'GET',
    json: {}
  };
  const sendObj = {
    title: 'My Blog'
  };

  request(requestOptions, function (error, response, body) {
    if (error) {
      console.log(error);
    }
    res.render('pages/blog', Object.assign({}, sendObj, body));
  });

I want to render not only articles but also an avatar on the pages/blog page. I tried to change the code like this:
const pathAPI = ['/api/blog', '/api/avatar'];
  const requestOptions = {
    blog: {
      url: apiOptions.server + pathAPI[0],
      method: 'GET',
      json: {}
    },
    avatar: {
      url: apiOptions.server + pathAPI[1],
      method: 'GET',
      json: {}
    }
  };

And do something like this request((requestOptions.blog, requestOptions.avatar), function (error, response, body) {...
But apparently this syntax is not correct.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Abcdefgk, 2018-04-22
@reyzele

Well, something like this

var locals = res.locals;
locals.title = 'My Blog';

const pathAPI = '/api/blog';
  const requestOptions = {
    url: apiOptions.server + pathAPI,
    method: 'GET',
    json: {}
  };

request(requestOptions, function (error, response, body) {
  if (error) {
    console.log(error);
  }
  locals.texts = body;

  // дальше смело второй запрос за аватарку и в его колбеке
  request(....
    ...
    locals.avatar = body
    res.render('pages/blog');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question