title: Express Server - Cannot POST /
body: <p>I recently followed a simple tutorial on how to build an Express server (<a href="https://codeforgeek.com/2014/06/express-nodejs-tutorial/" rel="noreferrer">https://codeforgeek.com/2014/06/express-nodejs-tutorial/</a>). </p>
  
  <p>I am trying to extend the code from this tutorial so that I can respond to post requests. I want to do this by updating a json file (that happens to be filled with 'user comments', and then rerendering at '/'</p>
  
  <p><strong>./server.js:</strong></p>
  
  <pre><code>var express = require('express');
  var app = express();
  
  // routing configuration
  require('./router/main')(app);
  
  // ejs configuration
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.engine('html', require('ejs').renderFile);
  
  // run the server
  var server = app.listen(8080, function(){
    console.log('Express server listening on port 8080');
  });
  </code></pre>
  
  <p><strong>./router/main.js (routers):</strong></p>
  
  <pre><code>var fs = require('fs');
  var ejs = require('ejs')
  
  module.exports = function(app){
  
    app.get('/', function(req, res){
      var comments = JSON.parse(fs.readFileSync(__dirname + '/../comments.json'));
      res.render('index.ejs', comments);
    });
  
    app.post('/', function(req, res){
      console.log('here in post');
      var name = req.body.name;
      var message = req.body.message;
      var newComment = {"name": name, "message": message};
      var comments = JSON.parse(fs.readFileSync(__dirname + '/../comments.json'));
      comments.push(newComment);
      fs.writeFileSync(__dirname + '/../comments.json', comments, 'utf8');
      //redirect to a 'get' on '/'
      res.redirect('/');
    });
  
    app.get('/about', function(req, res){
      res.render('about.html')
    });
  
  }
  </code></pre>
  
  <p><strong>./views/index.ejs:</strong></p>
  
  <pre><code>&lt;div&gt;
  
    &lt;div&gt;
      &lt;h1&gt; Joe's Forum &lt;/h1&gt;
      &lt;a href='/about'&gt; (about) &lt;/a&gt;
    &lt;/div&gt;
  
    &lt;div&gt;
      &lt;ul&gt;
      &lt;% comments.forEach( function(comment){ %&gt;
        &lt;li&gt;
          &lt;%= comment.name %&gt; : &lt;%= comment.message %&gt;
        &lt;/li&gt;
      &lt;% }); %&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
  
    &lt;h2&gt; Enter a new comment &lt;/h2&gt;
  
    &lt;form action='/' method="post"&gt;
      Enter your name: &lt;input type='text' name='name'&gt; &lt;br&gt;&lt;br&gt;
      Enter your message: &lt;input type='textarea' name='message'&gt; &lt;br&gt;&lt;br&gt;
      &lt;input type='submit' value='Submit'&gt;
    &lt;form&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>./comments.json:</strong></p>
  
  <pre><code>{
    "comments": [
      {"name":"Joe", "message" : "What advantages does Node.js afford the web developer?"},
      {"name": "John", "message": "Asynchronous IO helps us to keep our pages responsive even if the server is fetching data"}
    ]
  }
  </code></pre>
  
  <p>When I try to submit a new comment from my form, all I see is this:</p>
  
  <p>"Cannot POST /"</p>
  
  <p>Can someone please explain why I might be getting this error? Thanks</p>