代码之家  ›  专栏  ›  技术社区  ›  kinhs

我试图为我的react build to node/expressjs应用程序提供服务,但我遇到了这个错误

  •  0
  • kinhs  · 技术社区  · 2 年前

    我还在学习整个MERN stack技术。我试图遵循一个教程,将我的Create React应用程序构建文件提供给我的后端,但我不断遇到错误。我得到的错误是:

    Error: ENOENT: no such file or directory, stat 'C:\Users\kingsley\Desktop\blogfullstack \api\public\index.html'
    

    这是我的项目结构:

    enter image description here

    我在一个名为 public 在我的服务器文件夹里。这个 指数html 文件也在那里。

    在我的服务器上 应用程序。js 文件中,我根据课程教程编写了以下代码:

    app.use(express.json());
    
    app.use( express.static(path.join(__dirname, 'public', 'index.html')))
    

    在以上代码之后,我有了总共9个API端点,例如:

    app.use("/api/v1/auth", authRoute);
    
    app.get('/*', (req, res)=>{
        res.sendFile(path.join(__dirname, 'public', 'index.html'));
    })
    
    module.exports = app;
    

    我可能做错了什么?我很想学习并解决这个问题。

    0 回复  |  直到 2 年前