【问题标题】:The location of index.html on the Heroku serverHeroku 服务器上 index.html 的位置
【发布时间】:2019-08-01 01:10:35
【问题描述】:

我在server.js中写了index.html文件的位置。 但浏览器显示“未找到”。如何正确写入 index.html 文件的位置?

index.html 文件的位置。

(root)client->public->index.html

const express = require('express');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
var nodemailer = require('nodemailer');
const path = require('path');

const app = express();

// Serve static assets in production
if (process.env.NODE_ENV === 'production') {
  // Set static folder
  app.use(express.static('client/build'));

  app.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
  });
}


const port = process.env.PORT || 5001;
app.listen(port, ()=> console.log(`Server started on port ${port}`));

我希望浏览器显示首页,但 Heroku 日志返回以下行:

Error: ENOENT: no such file or directory, stat '/app/client/build/index.html

【问题讨论】:

  • 你也可以发布文件夹结构吗?
  • 这是文件夹结构。客户端(node_modules、public、resource、src、gitignore、package.json)、package.json、Procfile、server.js

标签: node.js reactjs express heroku


【解决方案1】:

我认为有两件事可能是您的问题的原因

  1. 您必须编写 res.sendFile('index.html') 只是因为您已将 'client/build' 声明为公共目录,因此您不再需要在应用程序中输入该路径。只写 index.html
  2. 如果这不是问题,可能您已经添加了 index.html 并且没有将它添加到 git 文件夹或没有提交它。也检查一下

【讨论】:

  • 在我在 github 上创建了一个存储库并使用它进行部署后,它就可以工作了。谢谢!
猜你喜欢
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 2015-02-12
  • 2015-04-26
  • 2011-09-13
相关资源
最近更新 更多