首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ENOENT:没有这样的文件或目录,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‘
EN

Stack Overflow用户
提问于 2020-04-25 05:53:06
回答 1查看 438关注 0票数 1

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

“环境:没有这样的文件或目录,请启动'/Users/sampai/Desktop/Repos/task_tracker/task_tracker/build/index.html‘”

下面是我在控制器文件中的get请求。

代码语言:javascript
复制
module.exports = function(router) {
    router.get("/api/tasks", (req, res) => {
        db.Task.findAll({}).then(data => {
            res.json(data);
        });
    });
}

下面是我的server.js文件

代码语言:javascript
复制
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模型

代码语言:javascript
复制
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;
}

有人能帮我找出为什么会出现这个错误吗?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-04-25 05:58:53

我想明白了,我只是在server.js中用build,index.html语句去掉了那个app.get,它起作用了!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61418039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档