[英]trouble deploying node.js app to heroku because of hard coded port number
我无法将角度种子应用程序( https://github.com/angular/angular-seed )部署到Heroku。 我相信问题是当heroku需要通过process.env.PORT变量动态分配端口时,端口号被硬编码到服务器启动脚本中。 查看heroku日志确认: Starting up http-server, serving ./ on port: 8000
。 我的Procfile包含web: npm start
,我的package.json文件包含
"scripts": {
"prestart": "npm install",
"start": "http-server -a localhost -p 8000"
}
如何更改它以便我的服务器仍然可以在具有foreman start
的本地开发环境中正常工作,但是在使用heroku时动态分配端口? 我想要它,以便当我在本地开发环境中键入foreman start
时,将在localhost:8000创建一个服务器,但是当我将应用程序部署到heroku时,Heroku将选择该端口。
我不是Node开发人员,但为什么不在start
脚本中使用Heroku配置的PORT编号?
即。 "start": "http-server -a localhost -p $PORT"
然后它将在Heroku上运行。 如果您在本地运行,只需export PORT=8000
,或者如果您使用foreman
运行,则将PORT=8000
放入.env
文件中,您就可以了。
如果没有通过用${PORT-8000}
替换$PORT
来设置PORT,你也可以添加bash magic来设置PORT。
(未测试)
有同样的问题。 到目前为止,在快递的帮助下解决了它。
添加了Procfile:
web: node web.js
添加了web.js.
var port = Number(process.env.PORT || 8000); var express = require('express'); var app = express(); app.use(express.static(__dirname + '/app')); var server = app.listen(port, function() { console.log('Listening on port %d', server.address().port); });
在package.json的dependencies部分添加了"express": "3.x"
更换
"start": "http-server -a localhost -p 8000"
同
"start": "node web.js"
然后提交,运行git push heroku master
并在它完成heroku open
如果你想在没有表达的情况下解决它,只使用节点http-server
模块,这里你需要在package.json
:
{
"dependencies": {
"http-server": "*"
},
"scripts": {
"start": "http-server ./ -p ${PORT:=5000} -c-1"
}
}
说明
http-server
(不应该在dev依赖项中) -p ${PORT:=8000}
将服务器绑定到可用于heroku实例的端口,该端口可在环境变量中找到。 如果不存在具有该名称的environement变量(例如,本地),则默认为8000
-a localhost
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.