index.js
// create an express app
const express = require("express");
const app = express();
// use the express-static middleware
app.use(express.static("public"));
// define the first route
app.get("/", function (req, res) {
res.send("<h1>Hello World!</h1>");
});
// start the server listening for requests
app.listen(process.env.PORT || 3000, () => console.log("Server is running..."));
package.json
{
"name": "SimpleApp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"engines": {
"node": "10.x",
"npm": "*"
}
}
资料
web: echo "I don't want a web process"
service: npm start
我已经在 heroku 中部署了它。
日志显示如下
2021-06-22T13:16:11.346614+00:00 app[service.1]: ------------------------------------------------------------------
2021-06-22T13:16:11.371025+00:00 app[service.1]: Server is running...
但是当我尝试加载页面时出现应用程序错误。
在本地运行良好
答案 0 :(得分:0)
您的网络测功机可能默认关闭。您需要将其打开才能运行您的 heroku 应用
答案 1 :(得分:0)
在声明中间件时检查你设置的目录是否正确
app.use(express.static("public"));
如果不是,则使用 __dirname 连接正确的目录,如下所示
const public = path.join(__dirname, "../public")
然后
app.use(express.static(public));