next.js - 如何设置为heroku nextjs
问题描述
我在 package.json 中使用此代码,我想部署到 heroku 应用程序
"scripts": {
"build": "cd app && next build",
"start": "npm run build && env NODE_ENV=production node app.js",
"dev": "nodemon --ignore app/ app.js"
},
我也试过:
"start": "next start -p $PORT",
我正在使用这个样板: https ://github.com/MustansirZia/next-express-bootstrap-boilerplate
解决方案
我相信它在错误的端口上运行。Heroku 会将端口作为环境变量提供给您,但您的应用需要从环境中获取端口,然后在启动时使用它。
在 app.js 中将其添加到顶部(将默认端口更改为您想要的任何内容)。它将从环境变量 PORT 中获取端口,如果不存在则默认为 3000:
const port = process.env.PORT || 3000;
然后在最底部,将最后一行更改为此
start(port);
推荐阅读
- python - list of price dictionaries to one price dictionary
- python - 为什么我的 start_request 函数没有在我的 scrapy 程序中调用我的解析函数?
- java - 列是 uuid 类型,但表达式是 Spark Scala 中不同的字符类型
- reactjs - 我应该什么时候在反应的更新生命周期中获取数据?
- ios - 如何为运行脚本设置自定义环境变量
- azure - Connect-AzAccount:资源暂时不可用 (management.azure.com:443)
- c - 服务器/客户端的分段错误在哪里?
- c++ - 无法找到我最近遇到的问题中缺少的极端案例
- python - Python 中的 YouTube 实时聊天:pageTokenInvalid 错误
- python - Googletrans 错误 - 握手操作超时