部署MernStack应用到Heroku时出现错误
创始人
2025-01-07 18:30:41
0次
- 在项目根目录下创建一个Procfile文件,文件内容为:
web: node src/server.js
- 使用以下命令将你的应用添加到Heroku中:
git add .
git commit -m "First commit"
heroku create 你的应用名
git push heroku master
- 确保你在项目的dependencies中添加了以下模块:
"dependencies": {
"express": "^4.17.1",
"mongoose": "^5.12.0",
"mongoose-id-validator": "^1.0.8"
}
- 在你的应用入口文件中确保你监听的端口号为process.env.PORT:
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(
Server started on port ${PORT}
));
- 确保你的应用连接的是Heroku提供的MongoDB数据库,而不是本地的数据库,你可以在Heroku中添加MongoDB插件并添加Config Vars来设置数据库访问路径。
- 最后,确保你的应用在Heroku中正确启动,你可以使用以下命令检查你的应用日志:
heroku logs --tail
相关内容