title: Express.js create URLs from MongoDB ID
body: <p>I'm new to node.js and express.js. I'm trying to create a todo app that works like this:
  I have a mongodb of Todos containing individual todo lists. These lists have tasks classified as unfinished or finished. For example:</p>
  <pre><code>{
  Todos: {
      Todo: {
          &quot;_id&quot; : ObjectId(&quot;5202b481d2184d390cbf6eca&quot;),
          finished: ['walk dog', 'do dishes'],
          unfinished: ['clean room']
      }
      Todo: {
          &quot;_id&quot; : ObjectId(&quot;5202b49ad2184d390cbf6ecb&quot;),
          finished: ['clean car', 'make dinner'],
          unfinished: ['write this damn web app ']
      }
  }
  }
  </code></pre>
  <p>I want to create new pages for each to do list, so that the lists are located as follows: <a href="http://website.com/5202b49ad2184d390cbf6ecb" rel="nofollow noreferrer">http://website.com/5202b49ad2184d390cbf6ecb</a></p>
  <p>So my question is, how can I create URLs on the fly like that?</p>
  <p>Thanks!</p>
  <hr />
  <h3>Edit:</h3>
  <p>I added this code to routes/index.js:</p>
  <pre><code>/* POST to New todo list service */
  router.post('/:id', function(req, res) {
      var db = req.db;
      var tasks = db.get('taskscollection');
      tasks.insert({
          &quot;id&quot;:req.params.id,
          &quot;finished&quot;:[&quot;&quot;],
          &quot;unfinished&quot;:[&quot;&quot;]
          }, function(err, doc) {
              if (err) {
                  res.send(&quot;there was a problem with adding a new tasklist&quot;);
                  }
              else {
                  res.location(&quot;/:id&quot;);
                  res.redirect(&quot;/:id&quot;);
                  }
              });
      });
  </code></pre>
  <p>and this code to index.jade:</p>
  <pre><code>   form#formNewTask(name='newtask', method='post', action='/:id')
       button#btnSubmit(type=&quot;submit&quot;) New Task List
  </code></pre>
  <p>But when I click the button, I get redirected to localhost:3000/:id and I get a 404 not found. What am I missing here?</p>
  <p>Also how do I create a jade template for the new page?</p>
  <hr />
  <h3>Edit 2:</h3>
  <p>I changed my index.js to look like this:</p>
  <pre><code>/* POST to New todo list service */
  router.post('/tasks/:_id', function(req, res) {
      var db = req.db;
      var tasks = db.get('taskscollection');
      tasks.insert({                                        
          &quot;finished&quot;:[&quot;&quot;],
          &quot;unfinished&quot;:[&quot;&quot;]
          }, function(err, doc) {
              if (err) {
                  res.send(&quot;there was a problem with adding a new task\
  list&quot;);
                  }
              else {
                  res.location(&quot;/tasks/&quot;+req.params.id);
                  res.redirect(&quot;/tasks/&quot;+req.params.id);
                  }
              });
      });
  </code></pre>
  <p>And my index.jade now has this:</p>
  <pre><code>   form#formNewTask(name='newtask', method='post', action='/tasks/'+_id)
       button#btnSubmit(type=&quot;submit&quot;) New Task List
  </code></pre>
  <p>Now when I click new task, it takes me to localhost:3000/tasks/undefined. Does this suggest that it's not creating a new entry in the DB? I think my front end jade file is wrong but I'm not sure.</p>