【问题标题】:ENOENT: no such file or directory, stat '/Users/sampai/Desktop/Repos/task_tracker/task_tracker/build/index.html'ENOENT:没有这样的文件或目录,stat '/Users/sampai/Desktop/Repos/task_tracker/task_tracker/build/index.html'
【发布时间】:2020-08-08 14:20:07
【问题描述】:

所以我正在尝试为我的全栈 MERN 应用程序(使用 mysql)创建后端路由。我的应用程序使用 sequelize 作为我的 ORM,当我尝试编写获取路径以从数据库中获取数据时,出现此错误

“ENOENT:没有这样的文件或目录,stat '/Users/sampai/Desktop/Repos/task_tracker/task_tracker/build/index.html'”

以下是我的控制器文件中的获取请求。

module.exports = function(router) {
    router.get("/api/tasks", (req, res) => {
        db.Task.findAll({}).then(data => {
            res.json(data);
        });
    });
}

下面是我的 server.js 文件

const express = require("express");
const app = express();
const path = require("path");
const PORT = process.env.PORT || 3000;
const db = require("./models");

app.use(express.static(path.join(__dirname, "build")));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.get("ping", function (req, res) {
    return res.send("pong");
})

app.get("*", function (req, res) {
    res.sendFile(path.join(__dirname, "build", "index.html"));
})

require("./controllers/taskController")(app);

db.sequelize.sync().then(function() {

    app.listen(PORT, () => {
        console.log("Your API server is now on PORT:", PORT);
    })

})

最后,下面是我的续集模型

const Sequelize = require("sequelize");

module.exports = function(sequelize, DataTypes){
    var Task = sequelize.define("Task", {
        id: {
            type: Sequelize.INTEGER(11),
            allowNull: false,
            autoIncrement: true,
            primaryKey: true
        },
        task: DataTypes.STRING
    });
return Task;
}

谁能帮我弄清楚为什么会出现这个错误?谢谢!

【问题讨论】:

    标签: mysql node.js reactjs express sequelize.js


    【解决方案1】:

    实际上我想通了,我只是用 server.js 中的 build、index.html 语句摆脱了 app.get 并且它起作用了!

    【讨论】:

      猜你喜欢
      • 2017-01-24
      • 2022-01-26
      • 2020-06-03
      • 2021-04-26
      • 2021-06-10
      • 2021-09-22
      • 2021-04-04
      • 2020-12-05
      • 2019-08-22
      相关资源
      最近更新 更多