title: Match a request parameter with a JSON Object with node.js and the express framework
body: <p>The Code works fine with the uncomment lines.
  But when i <strong>activate the <code>else</code></strong> Statement i get every times the 'not found' even there is a match between <code>req.params.code</code> and <code>data.airports[i].code</code>.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var express = require('express');
  var data = require('./data.json');
  var app = express();
  
  app.use(express.static('public'));
  app.set('view engine', 'jade');
  
  app.get('/', function (req, res) {
    res.render('index', { title: 'Startseite', message: 'index.html'});
  });
  
  app.get('/de-de/code/:code', function (req, res) {
    for (var i in data.airports) {
      if (req.params.code == data.airports[i].code) {
        res.render('iata-code', data.airports[i]);
  /*  } else {
        res.send('not found'); */
      };
    };
  });
  
  app.listen(80, function () {
    console.log('Example app is running!');
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Edit:</strong>
  I change the code to:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>app.get('/de-de/code/:code', function (req, res) {
    for (var i in data.airports) {
      if (req.params.code === data.airports[i].code) {
        res.status(200).render('iata-code', data.airports[i]);
    } else {
        res.status(404).send({ error: 'Something failed!' });
      };
    };
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>even i send the http status code 404 before the headers i get the error in my console: <code>Error: Can't set headers after they are sent.</code></p>
  
  <p><strong>Edit2:</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>app.get('/de-de/code/:code', function (req, res) {
    for (var i in data.airports) {
      if (req.params.code === data.airports[i].code) {
        res.writeHead(200, { "Content-Type": "text/html" });
        res.render('iata-code', data.airports[i]);
    } else {
        res.writeHead(404, { "Content-Type": "text/html" });
        res.write('Something failed!');
        res.end();
      };
    };
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Edit3:</strong> I set up an alternative way. But this is also not working. I am New to node.js but i still dind't find a solution.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var express = require('express');
  var data = require('./data.json');
  var airports = data.airports;
  var app = express();
  
  function filterData (reqCode) {
    var result = {};
    for (var i in airports) {
      console.log('-----');
      console.log(i + ': ' + reqCode + ' &lt;--&gt; ' + airports[i].code);
      console.log('-----');
      if (airports[i].code === reqCode) {
        result = airports[i];
      } else {
        result = {};
      };
    };
    return result;
    console.log(result);
  };
  
  app.get('/de-de/:code', function (req, res, next) {
    var reqCode = req.params.code;
    if (filterData(reqCode) === {}) next('route');
    else next();
  }, function (req, res, next) {
    res.write('200');
    res.end();
  });
  
  app.get('/de-de/:code', function (req, res, next) {
    res.write('404');
    res.end();
  });
  
  app.listen(80, function () {
    console.log('Example app is running! Cancel Server with CTRL + C');
  });</code></pre>
  </div>
  </div>
  </p>