title: How to upload a file in Node.js ? Error: Cannot POST /upload
body: <p><strong>PROBLEM:</strong></p>
  
  <p>I click on submit in my upload.ejs and I get the error: "Cannot POST /upload"</p>
  
  <hr>
  
  <p><strong>SOLUTION:</strong></p>
  
  <p>My problem was an architectural one more than a coding problem. </p>
  
  <p>I changed the structure of my project to solve the problem.</p>
  
  <p>I created a new file called "upload.js" to put the router code in. </p>
  
  <p>I also moved the "upload.ejs" to the root of the project. </p>
  
  <p>I updated my "app.js" to take into account the new router file "upload.js". </p>
  
  <p>I changed the form to use "upload.js".</p>
  
  <hr>
  
  <p><strong>CODE:</strong></p>
  
  <p>upload.js</p>
  
  <pre><code>var express = require("express");
  var router = express.Router();
  var flash = require("connect-flash");
  
  var firebase = require("firebase");
  
  var multer = require("multer");
  var upload = multer({dest:"./public/images/uploads/"});
  
  router.get("/", function(req, res, next){
      res.render("upload");
  });
  
  router.post("/", upload.single("image"), function(req, res, next){
  
      if (req.file){
          console.log("Uploading file...");
          var image = req.file.filename;
      }
      else {
          console.log("No file uploaded");
          var image = "noimage.jpg";
      }
  
      var post = {
          title: req.body.title,
          section: req.body.section,
          image: image,
      }
  
      var section = req.body.section.toLowerCase();
  
      firebase.database().ref("posts/"+section).push(post);
  
      req.flash("success_msg", "Post Created");
      res.redirect("/upload");
  
  
  });
  
  module.exports = router;
  </code></pre>
  
  <p>upload.ejs</p>
  
  <pre><code>&lt;form enctype="multipart/form-data" method="post" action="upload"&gt;
  </code></pre>