Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 第一个NodeJS应用程序-如何解决模块未找到和其他问题? 申请摘要_Javascript_Mysql_Node.js - Fatal编程技术网

Javascript 第一个NodeJS应用程序-如何解决模块未找到和其他问题? 申请摘要

Javascript 第一个NodeJS应用程序-如何解决模块未找到和其他问题? 申请摘要,javascript,mysql,node.js,Javascript,Mysql,Node.js,这是我第一次尝试编写NodeJS应用程序,这只是一个学习项目。它是一个简单的CRUDweb应用程序,用于管理项目。对于第一个项目,我试图遵循我在其他语言中学到的一些最佳实践,即尝试将应用程序的输入、处理和输出分开 问题 它不起作用。我知道有几个问题阻碍了它的正常运行,但我已经尽了最大努力。我不知道如何继续 问题#1: 找不到模块 Error: Cannot find module 'C:\wamp64\www\aaa-node-mvc\server.js' at Function.Mod

这是我第一次尝试编写NodeJS应用程序,这只是一个学习项目。它是一个简单的CRUDweb应用程序,用于管理项目。对于第一个项目,我试图遵循我在其他语言中学到的一些最佳实践,即尝试将应用程序的输入、处理和输出分开

问题 它不起作用。我知道有几个问题阻碍了它的正常运行,但我已经尽了最大努力。我不知道如何继续

问题#1: 找不到模块

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: []
问题#2-n: 由于我不知道如何解决问题1,我不确定如何解决我的下一个问题——我已经知道了

当我们点击/create路径时,如何执行插入,然后根据结果显示包含适当消息的页面

所以基本上,问题2是,我如何完成/create路线?我知道 在某个地方,我需要将用户重新引导到成功页面, 或显示一条故障信息

我不确定将createPage函数放置在何处以显示表单

我的文件夹结构 我决定这样构建我的应用程序:

文件描述
  • app/config/db.config.js-导出MySQL连接信息
  • app/models/db.js-连接到数据库
  • app/routes/item routes.js-定义“item”的所有路由
  • app/controllers/item controller.js-具有项目的方法(创建、删除等)
  • app/models/item model.js-处理实际的数据操作
  • app/views/index.ejs-主页
  • app/views/item add.ejs-创建项目页面
源代码 index.js

// 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, () => {
  console.log(`Server running on port: <a href='${port}'>RUN</a>`);
});
module.exports = {
    HOST: "localhost",
    USER: "root",
    PASSWORD: "",
    DB: "marketplace"
  };
module.exports = app => {
    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) => {
        res.render('index.ejs', {
            title: "Welcome to ITEMS Homepage"
        });
    },
};
const Item = require("../models/item-model.js");


// Create and Save a new Item
exports.create = (req, res) => {
    // 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) => {
        if (err)
            res.status(500).send({
                message: err.message || "Error inserting Item into the database."
            });
        else res.send(data);
    });
};
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) => {
    sql.query("INSERT INTO jeeps SET ?", newItem, (err, res) => {
      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;
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%=title%></title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

</head>

<body>
    <div class="container">
        <form action="" method="POST">
            <div class="form-group row">
                <label for="title" class="col-4 col-form-label">Title</label>
                <div class="col-8">
                    <input id="title" name="title" type="text" class="form-control" required="required">
                </div>
            </div>
            <div class="form-group row">
                <label for="price" class="col-4 col-form-label">Price</label>
                <div class="col-8">
                    <input id="price" name="price" type="text" class="form-control" required="required">
                </div>
            </div>
            <div class="form-group row">
                <label for="location" class="col-4 col-form-label">Location</label>
                <div class="col-8">
                    <input id="location" name="location" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="imgUrl" class="col-4 col-form-label">ImgUrl</label>
                <div class="col-8">
                    <input id="imgUrl" name="imgUrl" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="itemURL" class="col-4 col-form-label">Item URL</label>
                <div class="col-8">
                    <input id="itemURL" name="itemURL" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="is_deleted" class="col-4 col-form-label">Is Deleted</label>
                <div class="col-8">
                    <input id="is_deleted" name="is_deleted" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <div class="offset-4 col-8">
                    <button name="submit" type="submit" class="btn btn-primary">Submit</button>
                </div>
            </div>
        </form>
    </div>


</body>

</html>
/app/routes/item routes.js

// 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, () => {
  console.log(`Server running on port: <a href='${port}'>RUN</a>`);
});
module.exports = {
    HOST: "localhost",
    USER: "root",
    PASSWORD: "",
    DB: "marketplace"
  };
module.exports = app => {
    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) => {
        res.render('index.ejs', {
            title: "Welcome to ITEMS Homepage"
        });
    },
};
const Item = require("../models/item-model.js");


// Create and Save a new Item
exports.create = (req, res) => {
    // 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) => {
        if (err)
            res.status(500).send({
                message: err.message || "Error inserting Item into the database."
            });
        else res.send(data);
    });
};
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) => {
    sql.query("INSERT INTO jeeps SET ?", newItem, (err, res) => {
      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;
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%=title%></title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

</head>

<body>
    <div class="container">
        <form action="" method="POST">
            <div class="form-group row">
                <label for="title" class="col-4 col-form-label">Title</label>
                <div class="col-8">
                    <input id="title" name="title" type="text" class="form-control" required="required">
                </div>
            </div>
            <div class="form-group row">
                <label for="price" class="col-4 col-form-label">Price</label>
                <div class="col-8">
                    <input id="price" name="price" type="text" class="form-control" required="required">
                </div>
            </div>
            <div class="form-group row">
                <label for="location" class="col-4 col-form-label">Location</label>
                <div class="col-8">
                    <input id="location" name="location" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="imgUrl" class="col-4 col-form-label">ImgUrl</label>
                <div class="col-8">
                    <input id="imgUrl" name="imgUrl" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="itemURL" class="col-4 col-form-label">Item URL</label>
                <div class="col-8">
                    <input id="itemURL" name="itemURL" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="is_deleted" class="col-4 col-form-label">Is Deleted</label>
                <div class="col-8">
                    <input id="is_deleted" name="is_deleted" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <div class="offset-4 col-8">
                    <button name="submit" type="submit" class="btn btn-primary">Submit</button>
                </div>
            </div>
        </form>
    </div>


</body>

</html>
/app/controllers/item controller.js

// 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, () => {
  console.log(`Server running on port: <a href='${port}'>RUN</a>`);
});
module.exports = {
    HOST: "localhost",
    USER: "root",
    PASSWORD: "",
    DB: "marketplace"
  };
module.exports = app => {
    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) => {
        res.render('index.ejs', {
            title: "Welcome to ITEMS Homepage"
        });
    },
};
const Item = require("../models/item-model.js");


// Create and Save a new Item
exports.create = (req, res) => {
    // 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) => {
        if (err)
            res.status(500).send({
                message: err.message || "Error inserting Item into the database."
            });
        else res.send(data);
    });
};
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) => {
    sql.query("INSERT INTO jeeps SET ?", newItem, (err, res) => {
      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;
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%=title%></title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

</head>

<body>
    <div class="container">
        <form action="" method="POST">
            <div class="form-group row">
                <label for="title" class="col-4 col-form-label">Title</label>
                <div class="col-8">
                    <input id="title" name="title" type="text" class="form-control" required="required">
                </div>
            </div>
            <div class="form-group row">
                <label for="price" class="col-4 col-form-label">Price</label>
                <div class="col-8">
                    <input id="price" name="price" type="text" class="form-control" required="required">
                </div>
            </div>
            <div class="form-group row">
                <label for="location" class="col-4 col-form-label">Location</label>
                <div class="col-8">
                    <input id="location" name="location" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="imgUrl" class="col-4 col-form-label">ImgUrl</label>
                <div class="col-8">
                    <input id="imgUrl" name="imgUrl" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="itemURL" class="col-4 col-form-label">Item URL</label>
                <div class="col-8">
                    <input id="itemURL" name="itemURL" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="is_deleted" class="col-4 col-form-label">Is Deleted</label>
                <div class="col-8">
                    <input id="is_deleted" name="is_deleted" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <div class="offset-4 col-8">
                    <button name="submit" type="submit" class="btn btn-primary">Submit</button>
                </div>
            </div>
        </form>
    </div>


</body>

</html>
/app/models/item model.js

// 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, () => {
  console.log(`Server running on port: <a href='${port}'>RUN</a>`);
});
module.exports = {
    HOST: "localhost",
    USER: "root",
    PASSWORD: "",
    DB: "marketplace"
  };
module.exports = app => {
    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) => {
        res.render('index.ejs', {
            title: "Welcome to ITEMS Homepage"
        });
    },
};
const Item = require("../models/item-model.js");


// Create and Save a new Item
exports.create = (req, res) => {
    // 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) => {
        if (err)
            res.status(500).send({
                message: err.message || "Error inserting Item into the database."
            });
        else res.send(data);
    });
};
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) => {
    sql.query("INSERT INTO jeeps SET ?", newItem, (err, res) => {
      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;
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%=title%></title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

</head>

<body>
    <div class="container">
        <form action="" method="POST">
            <div class="form-group row">
                <label for="title" class="col-4 col-form-label">Title</label>
                <div class="col-8">
                    <input id="title" name="title" type="text" class="form-control" required="required">
                </div>
            </div>
            <div class="form-group row">
                <label for="price" class="col-4 col-form-label">Price</label>
                <div class="col-8">
                    <input id="price" name="price" type="text" class="form-control" required="required">
                </div>
            </div>
            <div class="form-group row">
                <label for="location" class="col-4 col-form-label">Location</label>
                <div class="col-8">
                    <input id="location" name="location" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="imgUrl" class="col-4 col-form-label">ImgUrl</label>
                <div class="col-8">
                    <input id="imgUrl" name="imgUrl" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="itemURL" class="col-4 col-form-label">Item URL</label>
                <div class="col-8">
                    <input id="itemURL" name="itemURL" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="is_deleted" class="col-4 col-form-label">Is Deleted</label>
                <div class="col-8">
                    <input id="is_deleted" name="is_deleted" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <div class="offset-4 col-8">
                    <button name="submit" type="submit" class="btn btn-primary">Submit</button>
                </div>
            </div>
        </form>
    </div>


</body>

</html>
/app/views/item add.ejs

// 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, () => {
  console.log(`Server running on port: <a href='${port}'>RUN</a>`);
});
module.exports = {
    HOST: "localhost",
    USER: "root",
    PASSWORD: "",
    DB: "marketplace"
  };
module.exports = app => {
    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) => {
        res.render('index.ejs', {
            title: "Welcome to ITEMS Homepage"
        });
    },
};
const Item = require("../models/item-model.js");


// Create and Save a new Item
exports.create = (req, res) => {
    // 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) => {
        if (err)
            res.status(500).send({
                message: err.message || "Error inserting Item into the database."
            });
        else res.send(data);
    });
};
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) => {
    sql.query("INSERT INTO jeeps SET ?", newItem, (err, res) => {
      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;
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%=title%></title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

</head>

<body>
    <div class="container">
        <form action="" method="POST">
            <div class="form-group row">
                <label for="title" class="col-4 col-form-label">Title</label>
                <div class="col-8">
                    <input id="title" name="title" type="text" class="form-control" required="required">
                </div>
            </div>
            <div class="form-group row">
                <label for="price" class="col-4 col-form-label">Price</label>
                <div class="col-8">
                    <input id="price" name="price" type="text" class="form-control" required="required">
                </div>
            </div>
            <div class="form-group row">
                <label for="location" class="col-4 col-form-label">Location</label>
                <div class="col-8">
                    <input id="location" name="location" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="imgUrl" class="col-4 col-form-label">ImgUrl</label>
                <div class="col-8">
                    <input id="imgUrl" name="imgUrl" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="itemURL" class="col-4 col-form-label">Item URL</label>
                <div class="col-8">
                    <input id="itemURL" name="itemURL" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label for="is_deleted" class="col-4 col-form-label">Is Deleted</label>
                <div class="col-8">
                    <input id="is_deleted" name="is_deleted" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <div class="offset-4 col-8">
                    <button name="submit" type="submit" class="btn btn-primary">Submit</button>
                </div>
            </div>
        </form>
    </div>


</body>

</html>
最后的想法 正如你所不知道的,我对NodeJS很陌生。此外,我还使用各种教程中的代码拼凑了这个应用程序。我想我已经接近解决问题了,但你会知道的更好。任何帮助都将不胜感激。我只是被卡住了


谢谢。

对于第一个问题,您正在尝试访问一个不存在的文件。由于您没有发布package.json文件,我只能猜测,但我认为它在开始脚本中。开始脚本应该是
node run index.js
而不是
node run server.js

对于第二个问题,我不知道您希望/create路由做什么,但是POST/item似乎已经实现了它的目的。只需将表单操作设置为“/item”即可创建新项。然后,您可以在
项中显示成功或失败消息。创建
回调函数

我不确定在哪里放置createPage函数来显示表单

也许可以在index.ejs文件中放置一个链接,该链接以(“/items”)的形式导航到页面


除了你的问题之外,你的代码还有很多错误
console.log
是你的朋友。登录您希望调用的函数,查看它们是否实际被调用,以及传递给函数的数据。从那里一路爬上去

您也不应该在单个文件中多次直接分配给
模块.exports
(就像您在./app/item routes.js中所做的那样)。此外,从文件导出内容时,更喜欢使用
module.exports
而不是
exports

阅读JavaScript中的作用域。函数参数只能在该函数中以及函数中声明的任何变量中访问(使用
var
关键字时有例外,因此更喜欢使用
let
const
)。因此,尝试访问./app/item-routes.js中的
app
属性将引发错误


我还建议将
getHomePage()
函数移出items路由文件,因为它与项目无关。可以直接将它放在基本
index.js
文件中,或者为它创建一个单独的路由。

Hi,您还可以提供包.json的内容吗?server.js文件的确切位置。您可能应该运行“node index.js”。谢谢。我已经做了你推荐的改变(我想)。这是我的更新代码:,我可以看到主页,但无法显示添加项目页面。不过我相信我取得了很大的进步。我在这里读到的技巧帮助很大。可以看到,您将主页呈现移到了index.js,尽管您没有从item-routes.js中取出getHomepage函数(第11-18行)。至于addItem页面,您只是忘记了键入要从中访问它的项目。只需将第6行更改为
app.get(“/item”,item.createPage)