Javascript 在heroku中部署应用程序时获取html时出错

Javascript 在heroku中部署应用程序时获取html时出错,javascript,node.js,express,heroku,Javascript,Node.js,Express,Heroku,这是我在express node.js中的index.js文件,在我将其部署到Heroku上之前,该应用程序一直运行良好,在生产过程中,它会向我抛出该错误,就像服务器找不到我的index.html一样 *-src ---index.html ---index.js * 您需要使用绝对文件路径:path.resolve(\uu dirname,'./index.html')。例如 文件结构: . ├── package-lock.json ├── package.json └── src

这是我在express node.js中的index.js文件,在我将其部署到Heroku上之前,该应用程序一直运行良好,在生产过程中,它会向我抛出该错误,就像服务器找不到我的index.html一样

*-src ---index.html ---index.js *


您需要使用绝对文件路径:
path.resolve(\uu dirname,'./index.html')
。例如

文件结构:

.
├── package-lock.json
├── package.json
└── src
    ├── index.html
    └── index.js
index.js

const express=require('express');
常数fs=要求('fs');
const path=require('path');
常量app=express();
use(express.urlencoded({extended:true}));
函数renderHTML(路径、响应){
fs.readFile(路径,null,函数(错误,数据){
如果(错误){
书面答复(404);
write('File not found!');
}否则{
响应。写入(数据);
}
response.end();
});
}
app.get('/',函数(req,res){
//renderHTML('./index.html',res);
renderHTML(path.resolve(uu dirname,'./index.html'),res);
});
app.listen(process.env.PORT | | 3000,()=>console.log('服务器正在监听端口3000');
index.html


文件
这是html
package.json

{
  "name": "61751970",
  "version": "1.0.0",
  "description": "",
  "main": "./src/index.js",
  "engines": {
    "node": "10.x"
  },
  "scripts": {
    "start": "node ./src/index.js"
  },
  "keywords": [],
  "author": "mrdulin",
  "license": "MIT",
  "dependencies": {
    "express": "^4.17.1"
  }
}
通过heroku logs--tail检查访问日志:

2020-05-19T06:53:49.000000+00:00应用程序[api]:构建成功
2020-05-19T06:53:53.715284+00:00应用程序[网站1]:
2020-05-19T06:53:53.715304+00:00应用程序[网站1]:>61751970@1.0.0启动/应用程序
2020-05-19T06:53:53.715304+00:00应用程序[web.1]:>node./src/index.js
2020-05-19T06:53:53.715304+00:00应用程序[网站1]:
2020-05-19T06:53:53.975992+00:00应用程序[web.1]:服务器正在侦听端口26862
2020-05-19T06:53:54.300011+00:00 heroku[web.1]:状态从启动更改为启动
2020-05-19T06:54:00.891168+00:00 heroku[路由器]:at=info method=GET path=“/”host=secret-mesa-39686.herokuapp.com请求\u id=9df765f8-e1a3-4015-a376-c6d9e0c6f645 fwd=“118.183.246.243”dyno=web.1 connect=0ms服务=20ms状态=200字节=367协议=https

您需要使用绝对文件路径:
path.resolve(\uu dirname,'./index.html')
。例如

文件结构:

.
├── package-lock.json
├── package.json
└── src
    ├── index.html
    └── index.js
index.js

const express=require('express');
常数fs=要求('fs');
const path=require('path');
常量app=express();
use(express.urlencoded({extended:true}));
函数renderHTML(路径、响应){
fs.readFile(路径,null,函数(错误,数据){
如果(错误){
书面答复(404);
write('File not found!');
}否则{
响应。写入(数据);
}
response.end();
});
}
app.get('/',函数(req,res){
//renderHTML('./index.html',res);
renderHTML(path.resolve(uu dirname,'./index.html'),res);
});
app.listen(process.env.PORT | | 3000,()=>console.log('服务器正在监听端口3000');
index.html


文件
这是html
package.json

{
  "name": "61751970",
  "version": "1.0.0",
  "description": "",
  "main": "./src/index.js",
  "engines": {
    "node": "10.x"
  },
  "scripts": {
    "start": "node ./src/index.js"
  },
  "keywords": [],
  "author": "mrdulin",
  "license": "MIT",
  "dependencies": {
    "express": "^4.17.1"
  }
}
通过heroku logs--tail检查访问日志:

2020-05-19T06:53:49.000000+00:00应用程序[api]:构建成功
2020-05-19T06:53:53.715284+00:00应用程序[网站1]:
2020-05-19T06:53:53.715304+00:00应用程序[网站1]:>61751970@1.0.0启动/应用程序
2020-05-19T06:53:53.715304+00:00应用程序[web.1]:>node./src/index.js
2020-05-19T06:53:53.715304+00:00应用程序[网站1]:
2020-05-19T06:53:53.975992+00:00应用程序[web.1]:服务器正在侦听端口26862
2020-05-19T06:53:54.300011+00:00 heroku[web.1]:状态从启动更改为启动
2020-05-19T06:54:00.891168+00:00 heroku[路由器]:at=info method=GET path=“/”host=secret-mesa-39686.herokuapp.com请求\u id=9df765f8-e1a3-4015-a376-c6d9e0c6f645 fwd=“118.183.246.243”dyno=web.1 connect=0ms服务=20ms状态=200字节=367协议=https