是否可以在没有web dyno的情况下将Node.js应用程序部署到Heroku? [英] Is it possible to deploy a Node.js application to Heroku without a web dyno?
问题描述
对于某些背景故事和参考,以下是一些Heroku文档页面的引用。
For some backstory and reference, here are some quotes from a few Heroku documentation pages.
当应用程序在根目录中有
package.json
文件时,将使用Heroku Node.js buildpack。
The Heroku Node.js buildpack is employed when the application has a
package.json
file in the root directory.
来自 Heroku Node.js支持>默认Web流程类型:
首先,Heroku查找 Procfile 指定您的流程类型。
如果没有 Procfile $ c在构建过程中,$ c>出现在应用程序的根目录中,您的Web进程将通过运行
npm start
,[...]
If no Procfile
is present in the root directory of your app during the build process, your web process will be started by running npm start
, [...]
Procfile包含许多流程类型声明,每个声明在一条新线上。每个流程类型都是在启动该流程类型的dyno时执行的命令声明。
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.
例如,如果 web
声明了进程类型,然后当启动此类型的dyno时,将执行与 web
进程类型关联的命令。例如,这可能意味着启动Web服务器。
For example, if a web
process type is declared, then when a dyno of this type is started, the command associated with the web
process type, will be executed. This could mean starting a web server, for example.
我有一个 package.json
根目录中的文件(将激活Node.js buildpack),我还包含了一个 Procfile
在根目录中包含以下内容:
I have a package.json
file in the root (which will activate the Node.js buildpack), and I've also included a Procfile
in the root with the following contents:
service: npm start
我认为不定义 web
dyno会导致它无法创建;只应按照 Procfile
中声明的配置创建服务
dyno。
I would assume that not defining a web
dyno would cause it to not be created; only the service
dyno should be created, following the configuration declared in the Procfile
.
相反,发生的事情是有效 网络
dyno 使用<$自动创建c $ c> npm start 和无效 服务
dyno是使用中的定义创建的Procfile
。然后我不得不:
Instead, what happened is that an active web
dyno was automatically created using npm start
and an inactive service
dyno was created using the definition in Procfile
. I then had to:
-
heroku ps:scale web = 0
-
heroku ps:规模服务= 1
heroku ps:scale web=0
heroku ps:scale service=1
我绝对可以想象想在Heroku上运行一个Node.js服务应用程序,该应用程序不接受任何传入连接,只接收传出连接。有没有办法配置Node.js buildpack,以便在未定义一个时自动创建 web
dyno?我查看了大量文档,寻找方法:(1)如此定义或(2) 删除自动生成 web
dyno;但是,我还没有找到任何东西。
I can definitely imagine wanting to run a Node.js "service" application on Heroku that does not accept any incoming connections, only making outgoing ones. Is there a way to configure the Node.js buildpack to not automatically create a web
dyno when one is not defined? I've looked through lots of documentation looking for a way to either: (1) define it as such or (2) remove the automatically generated web
dyno; but, I haven't found anything.
感谢您的帮助!
推荐答案
我最后在Heroku上打开了一张帮助台票。得到了他们的回复,所以我会在这里发布。谢谢Heroku的支持!
I ended up opening a helpdesk ticket with Heroku on this one. Got a response from them, so I'll post it here. Thanks Heroku support!
简短的回答是,不,目前你需要 heroku scale web = 0 service = 1
以便在没有公共 web
进程的情况下运行服务。有关更长的解释:
The short answer is that, no, currently you'll need to heroku scale web=0 service=1
in order to run a service without a public web
process. For a longer explanation:
早期,Node.js Buildpack检查是否存在 Procfile
和如果缺少,则使用 web:npm start
创建一个默认值。这使得在没有Web进程的情况下创建应用程序变得容易,因为您可以提供定义某些进程的 Procfile
,省略 web
从列表中。
Early on, the Node.js Buildpack checked for the presence of a Procfile
and, if missing, created a default one with web: npm start
. This made it easy to create apps without a web process, since you could just provide a Procfile
that defined some processes, omitting web
from the list.
然而,随着越来越多的用户需要构建包的数组而不是单个数组,该解决方案产生了问题。 Node是最受欢迎的第一个buildpack,因为Java,Python,PHP和Ruby应用程序经常使用它来构建前端资产。每当没有 Procfile
的应用程序首先运行Node,然后运行另一个buildpack时,Node会注入自己的默认 Procfile
( web:npm start
),然后第二个buildpack不会创建默认的 Procfile
,因为文件系统中已存在。因此,当应用程序中缺少一个默认的Procfile时,会为多语言应用程序创建下游问题。
However, as more and more users needed arrays of buildpacks instead of a single one, that solution created issues. Node is the most popular first buildpack, since it's frequently used by Java, Python, PHP, and Ruby apps to build front-end assets. Whenever an app without a Procfile
ran Node first, and another buildpack second, Node would inject its own default Procfile
(web: npm start
), and the second buildpack would then not create its default Procfile
as one already existed in the filesystem. So injecting a default Procfile when one is missing from the app creates problems downstream for multilingual apps.
所以,我们停止创建默认的Procfile,而是在bin / release 中使用default_process_types。这解决了后续buildpacks继承错误的默认Procfiles的问题,但由于 default_process_types
是扩展而不是被替换 code> Procfile 进程列表,在 Procfile
web 进程的应用程序>将合并默认的 web
进程。这就是为什么 web
即使没有也会出现web
输入 Procfile
。
So, we stopped creating a default Procfile and instead used default_process_types in bin/release. This fixes the issue of subsequent buildpacks inheriting incorrect default Procfiles, but since default_process_types
is extended rather than replaced by the Procfile
process list, apps without a web
process defined in their Procfile
will get the default web
process merged in. This is why web
appears even without a web
entry in Procfile
.
我们也不想让任何客户感到惊讶意外的账单。有些应用程序有许多进程类型,其中一些仅偶尔运行,一些仅限于单个实例,一些需要按比例放大,等等,因此将所有内容默认为1而不是0可能会导致额外的计费作为应用程序故障。这就是默认情况下非Web进程缩放为零的原因。
We also don't want to surprise any customers with unexpected bills. Some apps have many process types, some of which are only to be run occasionally, some limited to a single instance, some which need to be scaled up and down, etc, so defaulting everything to 1 rather than 0 could cause extra billing as well as app malfunctions. This is why non-web processes are scaled to zero by default.
这篇关于是否可以在没有web dyno的情况下将Node.js应用程序部署到Heroku?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!