title: Using the results of a GET request in Express router
body: <p>First Node/Express app.</p>
  
  <p>I'm having a hard time wrapping my head around on how to retrieve data from an endpoint and rendering it in the browser.</p>
  
  <p>I have a dataservice.js that gets a JSON object from an endpoint like this:</p>
  
  <pre><code>const http = require('http');
  
  getFinhockeyData = function() {
  
      http.get('http://tilastopalvelu.fi/ih/modules/mod_standings/helper/standings.php?statgroupid=3545', (res) =&gt; {
        console.log(`Got response: ${res.statusCode}`);
  
        var body = "";
  
        res.on('data', function (chunk) {
          body += chunk;
      })
  
        res.on('end', function () {
          var data = JSON.parse(body);
          console.log('data parsed.');
          console.log('first team name: ' + data.teams[0].TeamName);
          console.log(typeof data);
          return data;
      })
  
      }).on('error', (e) =&gt; {
        console.log(`Got error from Finhockey: ${e.message}`);
      });
  }
  
  module.exports.getFinhockeyData = getFinhockeyData;
  </code></pre>
  
  <p>Up until now things work and the <code>data</code> object can be console.logged and its content is usable.</p>
  
  <p>The router.js looks currently like this:</p>
  
  <pre><code>'use strict';
  
  const express = require('express');
  const async = require('async');
  const router = express.Router();
  const dataservice = require('./dataservice.js')
  
  
  router.get('/', function(req, res) {
      async.series([
              function(callback) {
                  getFinhockeyData(callback)
              }
          ],
          function(err, results) {
              console.log('start rendering');
              res.render('index', { data: data });
          })
  });
  
  module.exports = router;
  </code></pre>
  
  <p>When I run the app and refresh the / route, I can see from the console that the <code>getFinhockeyData</code> is called and the data object's content is available in dataservice.js's console.logs, but the browser window hangs and the <code>res.render</code> part is never reached.</p>
  
  <p>I understand that the rendering should be done only after the endpoint data request has finished (async.series usage), but it seems that I lack a fundamental understanding on how to actually use the result data from the getFinhockeyData function in the main route.</p>
  
  <p>Any advice on this? I'll be happy to provide more info if necessary.</p>