title: node js fetch data from body
body: <p>Hi i have just started to learn node.js and javascript. And I'm trying to fetch data from a url using node-fetch and then trying to json parse the data but i keep getting a return undefined.</p>
  <p>I'm trying to get the data from the variable game from the html below and then stringify and then parse the json but it returns undefined.</p>
  <p>And i was hoping maybe someone could help me figure out why?</p>
  <p>The html code is not mine i am trying to fetch data from a site i don't control.</p>
  <p>Page html:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  
    &lt;head&gt;
      &lt;meta charset=&quot;UTF-8&quot; /&gt;
      &lt;title&gt;Document Title&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div id=&quot;hud-div&quot;&gt;
        &lt;p&gt;Welcome to the blah blah blah blah text here&lt;/p&gt;
      &lt;/div&gt;
      &lt;script&gt;
        var game = new Game({
          stage: &quot;prod&quot;,
          servers: {
            v32306117: {
              id: &quot;v32306117&quot;,
              region: &quot;region 1&quot;,
              name: &quot;region #1&quot;,
              hostname: &quot;hostname1&quot;,
              port: 80,
              fallbackPort: 8000,
              population: 97,
              selected: false
            },
            v32306125: {
              id: &quot;v32306125&quot;,
              region: &quot;region 2&quot;,
              name: &quot;region #2&quot;,
              hostname: &quot;hostname2&quot;,
              port: 80,
              fallbackPort: 8000,
              population: 38,
              selected: false
            }
          },
          userGroup: 0
        });
  
        game.init(function() {
          game.assetManager.load([{
            &quot;name&quot;: &quot;\/asset\/image\/map\/grass.png&quot;,
            &quot;url&quot;: &quot;\/asset\/image\/map\/grass.png&quot;
          }]);
  
          game.debug.init();
          game.run();
        });
  
      &lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Fetch function:</p>
  <pre><code>    const fetch = require(&quot;node-fetch&quot;);
  
      async function serversFetch() {
        try {
          const servers = await fetch(&quot;https://get-servers.herokuapp.com/&quot;);
          const data = await servers.text();
          const servers_data = data.substring(
            data.lastIndexOf(&quot;var game =&quot;) + 20,
            data.lastIndexOf(&quot;game.init&quot;) - 10
          );
  
          return JSON.stringify(servers_data);
        } catch (error) {
          console.log(error);
        }
      }
  
      (async () =&gt; {
        const data = await serversFetch();
        console.log('data', data);
  
        const info = JSON.parse(data);
        console.log('info', info.servers); // returns undefined
      })()
  </code></pre>
  <p>if i console log <code>info</code> without <code>servers</code> i get a response like below but when i try to console log <code>info.servers</code> it comes back undefined.</p>
  <pre><code>info {
          stage: &quot;prod&quot;,
          servers: {
            v32306117: {
              id: &quot;v32306117&quot;,
              region: &quot;region 1&quot;,
              name: &quot;region #1&quot;,
              hostname: &quot;hostname1&quot;,
              port: 80,
              fallbackPort: 8000,
              population: 97,
              selected: false
            },
            v32306125: {
              id: &quot;v32306125&quot;,
              region: &quot;region 2&quot;,
              name: &quot;region #2&quot;,
              hostname: &quot;hostname2&quot;,
              port: 80,
              fallbackPort: 8000,
              population: 38,
              selected: false
            }
          },
          userGroup: 0
        }
  </code></pre>
  <p>I have made an example site using herokuapp for anyone that wants to help.</p>