title: Node/Express external API call from internal endpoint
body: <p>In my node/Express app, I want to make a request to an external API using a secret API key. Following <a href="https://stackoverflow.com/questions/33398903/node-process-env-variables-empty">this question</a> I asked, I decided to build an internal endpoint and make a request from that endpoint (where I have access to my server-side ENV variables) to the external API.</p>
  
  <p>So I created a wrapper class that would interact with the API like so:</p>
  
  <pre><code>new FreeImages('search-keyword').fetch( callback(apiResponse) )
  </code></pre>
  
  <p>And I created an internal endpoint to grab that response and return it as requested internally. So my endpoint should look like this:</p>
  
  <pre><code>router.get '/images', (req, res, next) -&gt;
    new FreeImages('nature').fetch (images) -&gt;
      res.json(images)
  </code></pre>
  
  <p>But that doesn't work, and neither does this:</p>
  
  <pre><code>router.get '/images', (req, res, next) -&gt;
    new FreeImages('nature').fetch (images) -&gt;
      res.json({ unrelated: 'json' })
  </code></pre>
  
  <p>But this does:</p>
  
  <pre><code>router.get '/images', (req, res, next) -&gt;
    res.json({ unrelated: 'json' })
  </code></pre>
  
  <p>Which leads me to believe (since my FreeImages class has worked in other contexts) that the issue is asynchrony -- the internal endpoint tries to return before receiving the data to return. </p>
  
  <p>Am I right about what's wrong here? How can I get around it and route my API call through an internal endpoint that obscures my key?</p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>Here's the FreeImages class:</p>
  
  <pre><code>$ = require('jquery')
  _ = require('lodash')
  
  class FreeImages
  
    constructor: (@search) -&gt;
  
    fetch: (callback) -&gt;
      $.ajax
        crossDomain: true
        type: 'GET'
        url: @_dataUrl()
        dataType: 'jsonp'
        jsonpCallback: 'jsonCallback'
        success: (result) -&gt;
          images = _.reduce(result.sources, ((array, source) -&gt;
            array.concat source.result
          ), [])
  
          callback images
        error: (result) -&gt;
          console.log 'Something went wrong fetching images.'
          false
      true
  
    _dataUrl: -&gt;
      "http://freeimages.pictures/api/user/#{ process.env.FREE_IMAGES_API_KEY }/?keyword=#{ @search }&amp;format=jsonp"
  
  module.exports = FreeImages
  </code></pre>
  
  <p>I don't think it's at fault, because I used it previously directly from the front-end (with a hard-coded API key), and it worked fine. The <code>console.log</code> in the error case doesn't seem to get hit, either. And the server shows no error messaging that's particularly useful:</p>
  
  <pre><code>GET /images 500 27.680 ms - 2566
  </code></pre>
  
  <p>Also, if I <code>console.log</code> above and below the <code>FreeImages.fetch</code> line, the former, but not the latter, is printed to the server logs. So it seems to not be entering the callback at all.</p>
  
  <p><strong>EDIT2</strong></p>
  
  <p>If I keep console logging, it gets right up to the <code>$.ajax</code> call in <code>FreeImages</code>, but not beyond it. Not sure what's going wrong, as the URL looks right there, and the code is identical to what was working earlier. </p>