title: First NodeJS Application - How do I resolve MODULE NOT FOUND, and other issue?
body: <h2>Application Summary</h2>
  
  <p>This is my first attempt at writing a NodeJS application, and this is a learning project only. It's a simple <strong>CRUD</strong> web application to manage items. For this first project, I am trying to follow some best practices I have learned in other languages, namely, trying to keep separate the <em>input, processing, and output</em> of the application.</p>
  
  <h2>The Problem(s)</h2>
  
  <p>It don't work. I know there are a couple issues preventing this from running properly, but I have gone as far as I can. I don't know how to proceed.</p>
  
  <p><strong>Problem #1:</strong>
  MODULE NOT FOUND</p>
  
  <pre><code>Error: Cannot find module 'C:\wamp64\www\aaa-node-mvc\server.js'
      at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)       
      at Function.Module._load (internal/modules/cjs/loader.js:864:27)
      at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
  
      at internal/main/run_main_module.js:18:47 {
    code: 'MODULE_NOT_FOUND',
    requireStack: []
  </code></pre>
  
  <p><strong>Problem #2 - n:</strong>
  Since I can't figure out how to get by problem #1, I am unsure of how to address my next problem - which I already know. </p>
  
  <p><strong>When we hit the /create route, how do I perform the insert, and THEN based on the outcome, display a page with appropriate message</strong></p>
  
  <blockquote>
    <p>So basically, Problem #2 is, how do I finish the <strong>/create</strong> route? I know
    somewhere I need to re-direct the user either to a <strong>SUCCESS</strong> page,
    or display a <strong>failure message</strong>. </p>
  </blockquote>
  
  <p>I am unsure where to put the <strong>createPage</strong> function to display the form.</p>
  
  <h3>My Folder Structure</h3>
  
  <p>I decided on structuring my app like this:</p>
  
  <p><a href="https://i.stack.imgur.com/jBkeP.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jBkeP.jpg" alt="Files"></a></p>
  
  <h3>File Descriptions</h3>
  
  <ul>
  <li><strong>app / config / db.config.js</strong> - Exports MySQL connection info</li>
  <li><strong>app / models / db.js</strong> - Connects to the Database</li>
  <li><strong>app / routes / item-routes.js</strong> - Defines all routes for an 'item'</li>
  <li><strong>app / controllers / item-controller.js</strong> - Has methods for an item ( create, delete, etc )</li>
  <li><strong>app / models / item-model.js</strong> - Handles the actual data manipulation</li>
  <li><strong>app / views / index.ejs</strong> - The home page</li>
  <li><strong>app / views / item-add.ejs</strong> - The create item page</li>
  </ul>
  
  <h3>Source Code</h3>
  
  <p><strong>index.js</strong></p>
  
  <pre><code>// Dependencies
  const bodyParser    = require("body-parser");
  const app           = express();
  const {getHomePage} = require('./app/routes/item-routes');
  
  // Enable content-types
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: true }));
  
  // Define Routes
  app.get('/', getHomePage);
  require("./app/routes/item-routes.js")(app);
  
  // Listen for requests
  const PORT = process.env.PORT || 3000;
  app.listen(PORT, () =&gt; {
    console.log(`Server running on port: &lt;a href='${port}'&gt;RUN&lt;/a&gt;`);
  });
  </code></pre>
  
  <p><strong>./app/config/db.js</strong></p>
  
  <pre><code>module.exports = {
      HOST: "localhost",
      USER: "root",
      PASSWORD: "",
      DB: "marketplace"
    };
  </code></pre>
  
  <p><strong>./app/routes/item-routes.js</strong></p>
  
  <pre><code>module.exports = app =&gt; {
      const items = require("../controllers/item-controller.js");
  }
  
   // ROUTE FOR: Creating a new item
   app.post("/items", items.create);
   app.get("/items", items.createPage);
  
  module.exports = {
      getHomePage: (req, res) =&gt; {
          res.render('index.ejs', {
              title: "Welcome to ITEMS Homepage"
          });
      },
  };
  </code></pre>
  
  <p><strong>./app/controllers/item-controller.js</strong></p>
  
  <pre><code>const Item = require("../models/item-model.js");
  
  
  // Create and Save a new Item
  exports.create = (req, res) =&gt; {
      // Validate request
      if (!req.body) {
          res.status(400).send({
              message: "Item can not be empty!"
          });
      }
  
      const item = new Item({
          title: req.body.title,
          price: req.body.price,
          location: req.location,
          imgUrl: req.body.imgUrl,
          itemURL: req.body.itemURL,
          is_deleted: req.body.is_deleted
      });
  
      // Insert Item
      Item.create(item, (err, data) =&gt; {
          if (err)
              res.status(500).send({
                  message: err.message || "Error inserting Item into the database."
              });
          else res.send(data);
      });
  };
  </code></pre>
  
  <p><strong>./app/models/item-model.js</strong></p>
  
  <pre><code>const sql = require("./db.js");
  
  // Constructor
  const Item = function(item) {
      this.title = item.title; 
      this.price= req.body.price;
      this.location= req.location;
      this.imgUrl =req.body.imgUrl;
      this.itemURL= req.body.itemURL;
      this.is_deleted= req.body.is_deleted;  
    };
  
  // Create and Save a new Item
  Item.create = (newItem, result) =&gt; {
      sql.query("INSERT INTO jeeps SET ?", newItem, (err, res) =&gt; {
        if (err) {
          console.log("error: ", err);
          result(err, null);
          return;
        }
  
        console.log("created item: ", { id: res.insertId, ...newItem });
        result(null, { id: res.insertId, ...newItem });
      });
    };
  
  
  module.exports = Item;
  </code></pre>
  
  <p><strong>./app/views/item-add.ejs</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;title&gt;&lt;%=title%&gt;&lt;/title&gt;
      &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"&gt;
      &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;div class="container"&gt;
          &lt;form action="" method="POST"&gt;
              &lt;div class="form-group row"&gt;
                  &lt;label for="title" class="col-4 col-form-label"&gt;Title&lt;/label&gt;
                  &lt;div class="col-8"&gt;
                      &lt;input id="title" name="title" type="text" class="form-control" required="required"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group row"&gt;
                  &lt;label for="price" class="col-4 col-form-label"&gt;Price&lt;/label&gt;
                  &lt;div class="col-8"&gt;
                      &lt;input id="price" name="price" type="text" class="form-control" required="required"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group row"&gt;
                  &lt;label for="location" class="col-4 col-form-label"&gt;Location&lt;/label&gt;
                  &lt;div class="col-8"&gt;
                      &lt;input id="location" name="location" type="text" class="form-control"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group row"&gt;
                  &lt;label for="imgUrl" class="col-4 col-form-label"&gt;ImgUrl&lt;/label&gt;
                  &lt;div class="col-8"&gt;
                      &lt;input id="imgUrl" name="imgUrl" type="text" class="form-control"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group row"&gt;
                  &lt;label for="itemURL" class="col-4 col-form-label"&gt;Item URL&lt;/label&gt;
                  &lt;div class="col-8"&gt;
                      &lt;input id="itemURL" name="itemURL" type="text" class="form-control"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group row"&gt;
                  &lt;label for="is_deleted" class="col-4 col-form-label"&gt;Is Deleted&lt;/label&gt;
                  &lt;div class="col-8"&gt;
                      &lt;input id="is_deleted" name="is_deleted" type="text" class="form-control"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group row"&gt;
                  &lt;div class="offset-4 col-8"&gt;
                      &lt;button name="submit" type="submit" class="btn btn-primary"&gt;Submit&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <h2>SQL</h2>
  
  <pre><code>-- phpMyAdmin SQL Dump
  -- version 4.9.2
  -- https://www.phpmyadmin.net/
  --
  -- Host: 127.0.0.1:3306
  -- Generation Time: May 13, 2020 at 04:11 AM
  -- Server version: 5.7.28
  -- PHP Version: 7.3.12
  
  SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
  SET AUTOCOMMIT = 0;
  START TRANSACTION;
  SET time_zone = "+00:00";
  
  
  /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
  /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
  /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
  /*!40101 SET NAMES utf8mb4 */;
  
  --
  -- Database: `marketplace`
  --
  
  -- --------------------------------------------------------
  
  --
  -- Table structure for table `jeeps`
  --
  
  DROP TABLE IF EXISTS `jeeps`;
  CREATE TABLE IF NOT EXISTS `jeeps` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `title` varchar(255) DEFAULT NULL,
    `price` varchar(255) DEFAULT NULL,
    `location` varchar(255) DEFAULT NULL,
    `itemURL` text,
    `imgUrl` text,
    `is_deleted` tinyint(1) NOT NULL,
    `created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
  ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
  
  --
  -- Dumping data for table `jeeps`
  --
  
  INSERT INTO `jeeps` (`id`, `title`, `price`, `location`, `itemURL`, `imgUrl`, `is_deleted`, `created_date`) VALUES
  (1, '1999 Jeep Wrangler', '$5,998', 'Tampa', 'http://localhost/fgdgdgfd', 'http://localhost/fgdgdgfd/ffd.jpg', 0, '2020-05-09 17:48:21'),
  (2, '2001 Jeep Wrangler Sport', '$9000', 'Mass', 'http://www.', 'http://img', 0, '2020-05-12 03:35:35');
  COMMIT;
  
  /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
  /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
  /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
  
  </code></pre>
  
  <p><strong>Final Thoughts</strong>
  As you have no-undoubtedly figured out, I am VERY new to NodeJS. Also, I have pieced this app together by using code from various tutorials. I would like to think I am close to getting the problem resolved, but you would know better. Any help would be greatly appreciated. I am just plain stuck.</p>
  
  <p>Thank you.</p>