gpt4 book ai didi

mysql - ENOENT : no such file or directory, 统计 '/Users/sampai/Desktop/Repos/task_tracker/task_tracker/build/index.html'

转载 作者:行者123 更新时间:2023-12-03 22:19:33 25 4
gpt4 key购买 nike

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

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

下面是我在 Controller 文件中的 get 请求。

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);
})

})

最后,下面是我的 Sequelize 模型
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;
}

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

最佳答案

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

关于mysql - ENOENT : no such file or directory, 统计 '/Users/sampai/Desktop/Repos/task_tracker/task_tracker/build/index.html',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61418039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com