对于一些背景故事和引用,这里引用了一些 Heroku 文档页面。
来自Heroku Node.js Support > Activation :
The Heroku Node.js buildpack is employed when the application has a
package.json
file in the root directory.
来自 Heroku Node.js Support > Default web process type :
First, Heroku looks for a Procfile specifying your process types.
If no
Procfile
is present in the root directory of your app during the build process, your web process will be started by runningnpm start
, [...]
来自 Process Types and the Procfile > Process types as templates :
A Procfile contains a number of process type declarations, each on a new line. Each process type is a declaration of a command that is executed when a dyno of that process type is started.
For example, if a
web
process type is declared, then when a dyno of this type is started, the command associated with theweb
process type, will be executed. This could mean starting a web server, for example.
我在根目录中有一个 package.json
文件(它将激活 Node.js buildpack),我还在根目录中包含了一个 Procfile
以下内容:
service: npm start
我假设不定义 web
dyno 会导致它无法创建;仅应创建 service
dyno,遵循 Procfile
中声明的配置。
相反,发生的事情是使用 npm start
和 自动 创建了一个 active web
dyno strong>inactive service
dyno 是使用 Procfile
中的定义创建的。然后我不得不:
heroku ps:scale web=0
heroku ps:scale service=1
我完全可以想象想要在 Heroku 上运行一个 Node.js“服务”应用程序,它不接受任何传入连接,只进行传出连接。有没有一种方法可以将 Node.js 构建包配置为在未定义时不自动创建 web
dyno?我查阅了很多文档以寻找一种方法来:(1) 如此定义它或(2) 自动删除生成 web
dyno;但是,我还没有找到任何东西。
感谢您的帮助!
最佳答案
我最终在 Heroku 上开了一张帮助台票。收到了他们的回复,所以我会在这里发布。感谢 Heroku 的支持!
简短的回答是,不,目前您需要 heroku scale web=0 service=1
以便在没有公共(public) web
进程的情况下运行服务.更详细的解释:
早期,Node.js Buildpack 检查是否存在 Procfile
,如果不存在,则使用 web: npm start
创建一个默认文件。这使得在没有网络进程的情况下创建应用程序变得容易,因为您可以只提供一个定义了一些进程的 Procfile
,从列表中省略 web
。
但是,随着越来越多的用户需要构建包阵列而不是单个构建包,该解决方案产生了问题。 Node 是最流行的第一个构建包,因为 Java、Python、PHP 和 Ruby 应用程序经常使用它来构建前端 Assets 。每当没有 Procfile
的应用首先运行 Node,然后再运行另一个 buildpack 时,Node 会注入(inject)它自己的默认 Procfile
(web: npm start
),并且第二个 buildpack 将不会创建其默认的 Procfile
,因为它已经存在于文件系统中。因此,在应用程序中缺少默认 Procfile 时注入(inject)默认 Procfile 会给多语言应用程序的下游带来问题。
所以,我们 stopped creating a default Procfile and instead used default_process_types in bin/release .这解决了后续构建包继承不正确的默认 Procfile 的问题,但由于 default_process_types
是 extended 而不是 replaced Procfile
进程列表,在其 Procfile
中没有定义 web
进程的应用程序将获得默认的 web
进程合并。这就是为什么 web
即使在 Procfile
中没有 web
条目也会出现。
我们也不想让任何客户收到意想不到的账单。有些应用程序有很多进程类型,其中一些只是偶尔运行,一些仅限于单个实例,一些需要放大和缩小等,所以将所有内容默认为 1 而不是 0 也可能导致额外计费作为应用程序故障。这就是默认情况下将非 Web 进程缩放为零的原因。
关于javascript - 是否可以在没有 web dyno 的情况下将 Node.js 应用程序部署到 Heroku?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40265699/