title: Express - Nodejs external rest api call
body: <p>I want to make a backend call to an external api's and populate my page with the results. What is the best way to do this?</p>
  
  <p>The "request.get" call is asynchronous, so I understand the code below is erroneous. However, I have written it in that fashion so that I can explain what I want to actually do.  </p>
  
  <p>Further, I may have 5-6 external api, is there a way to make this asynchronous for every api but synchronous get call?</p>
  
  <p>This is how my current code looks like:</p>
  
  <pre><code>var express = require('express');
  var request = require('request');
  var router = express.Router();
  
  /* GET home page. */
  router.get('/', function(req, res, next) {
    var body = getRawApiResponse("someURL");
    console.log("Index &gt;" + body);
    res.render('index', { title: 'Express', api: "some", body: body});
  
  });
  
  
  function getRawApiResponse(api){
    request.get({
      uri: api,
    },
    function(error, response, body){
      if (!error &amp;&amp; response.statusCode === 200) {
          console.log("Index &gt; Raw Api Response: " + body);
        } else {
          console.log(error);
        }
      });
  
  }
  </code></pre>