title: node.js & express, redirect or send file after async db search
body: <p>I am trying to write routes for my node.js &amp; express application and having a little problem.</p>
  
  <p>The logic is this:</p>
  
  <ol>
  <li>user enter website with special tag id <code>/6758HDE</code></li>
  <li>i do async check in db if tag already exists</li>
  <li>if tag exists i roll new unique tag and set url with it</li>
  <li>if tag doesnt exist i allow current tag and set url with it</li>
  </ol>
  
  <p>so far this is my code:</p>
  
  <pre><code>app.get('/*', function(req, res) {
      check_in_db(req.url, function(result) {
          if(result) {
              res.sendFile(__dirname + '/index.html');
          } else {
              res.redirect('/' + roll_new_id());
          }
      });
  });
  </code></pre>
  
  <p>I guess that code is not working because it does async db search while headers must be sent right away, i remember that there was something like <code>next()</code> to deal with this kind of situation but my knowledge is very weak, maybe someone could point me in right track and show how to change my code so it will work as intended.</p>