title: Serving dynamic URLs with express and mongodb
body: <p>I'm building a site that has somewhat reddit-like functionality. I want user-submitted content to get its own page. Each submission is assigned a 5 character ID that I want to be in the URL for that page. </p>
  
  <p>I've got this function in the router file which renders a page called titles:</p>
  
  <pre><code>exports.titles = function(req, res){
  i = 0
  read(function(post){
      url = post[i].URL;
      res.render('titles', {title: post[i].title, url: post[i].URL});
  });
  
  };
  </code></pre>
  
  <p>It is served by this statement in app.js:</p>
  
  <pre><code>app.get('/titles', home.titles); //home.js is the router file
  </code></pre>
  
  <p>The titles page has a link with the text post.title and the URL post.URL. When a user clicks on the link (e.g. domain.com/12345) they should be taken to a page called content with the content post.body. </p>
  
  <p>How do I a)pass the URL back to my app.js file to include in an app.get, b) include the app.get function in this router file, or c) solve this in any other way?</p>
  
  <p>Edit: I do have an object 'titles' that is a mongodb collection, but it is in a different module. No reason I can't add it to the router though. </p>
  
  <p>Edit: I tried adding this to app.js to see if it would work:</p>
  
  <pre><code>app.get('/:id', function(req, res){
    return titles.findOne({ id: req.params.id }, function (err, post) {
      if (err) throw(err); 
  
      return res.render('content', {title: post.title, content: post.body});
     });
  });
  </code></pre>
  
  <p>Edit: I got it to work. All I did was format the title so that it would look like domain.com/titles/12345 and change app.get('/:id', to app.get('/titles/:id, ...</p>