启动时自动启动 node.js 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20445599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:28:32  来源:igfitidea点击:

Auto start node.js server on boot

node.js

提问by Bachalo

Can any node.js experts tell me how I might configure node JS to autostart a server when my machine boots? I'm on Windows

任何 node.js 专家都可以告诉我如何配置 node JS 以在我的机器启动时自动启动服务器吗?我在 Windows 上

回答by talles

This isn't something to configure in node.js at all, this is purely OS responsibility (Windows in your case). The most reliable way to achieve this is through a Windows Service.

这根本不是要在 node.js 中配置的东西,这纯粹是操作系统的责任(在您的情况下是 Windows)。实现这一目标的最可靠方法是通过 Windows 服务。

There's this super easymodule that installs a node script as a windows service, it's called node-windows(npm, github, documentation). I've used before and worked like a charm.

有这个超级简单的模块可以将节点脚本安装为 Windows 服务,它被称为node-windowsnpmgithubdocumentation)。我以前用过,效果很好。

var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
  name:'Hello World',
  description: 'The nodejs.org example web server.',
  script: 'C:\path\to\helloworld.js'
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});

svc.install();


p.s.

ps

I found the thing so useful that I built an even easier to use wrapper around it (npm, github).

我发现它非常有用,以至于我围绕它构建了一个更易于使用的包装器(npmgithub)。

Installing it:

安装它:

npm install -g qckwinsvc

Installing your service:

安装您的服务:

> qckwinsvc
prompt: Service name: [name for your service]
prompt: Service description: [description for it]
prompt: Node script path: [path of your node script]
Service installed

Uninstalling your service:

卸载您的服务:

> qckwinsvc --uninstall
prompt: Service name: [name of your service]
prompt: Node script path: [path of your node script]
Service stopped
Service uninstalled

回答by durum

If you are using Linux, macOS or Windows pm2is your friend. It's a process manager that handle clusters very well.

如果您使用 Linux,macOS 或 Windows pm2是您的朋友。它是一个可以很好地处理集群的进程管理器。

You install it:

你安装它:

npm install -g pm2

Start a cluster of, for example, 3 processes:

启动一个集群,例如 3 个进程:

 pm2 start app.js -i 3

And make pm2 starts them at boot:

并且 make pm2 在启动时启动它们:

 pm2 startup

It has an API, an even a monitor interface:

它有一个 API,甚至是一个监视器接口

AWESOME

惊人的

Go to github and read the instructions. It's easy to use and very handy. Best thing ever since forever.

转到 github 并阅读说明。它易于使用且非常方便。最好的事情,从此永远

回答by Chetan Bhasin

If I'm not wrong, you can start your application using command line and thus also using a batch file. In that case it is not a very hard task to start it with Windows login.

如果我没错,您可以使用命令行启动您的应用程序,因此也可以使用批处理文件。在这种情况下,使用 Windows 登录启动它并不是一项艰巨的任务。

You just create a batch file with the following content:

您只需创建一个包含以下内容的批处理文件:

node C:\myapp.js

and save it with .bat extention. Here myapp.js is your app, which in this example is located in C: drive (spcify the path).

并使用 .bat 扩展名保存它。这里 myapp.js 是你的应用程序,在这个例子中它位于 C: 驱动器(指定路径)。

Now you can just throw the batch file in your startup folder which is located at C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

现在,您可以将批处理文件放入位于 C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 的启动文件夹中

Just open it using %appdata% in run dailog box and locate to >Roaming>Microsoft>Windows>Start Menu>Programs>Startup

只需在运行对话框中使用 %appdata% 打开它并找到>Roaming>Microsoft>Windows>Start Menu>Programs>Startup

The batch file will be executed at login time and start your node application from cmd.

批处理文件将在登录时执行并从 cmd 启动您的节点应用程序。

回答by robereng

This can easily be done manually with the Windows Task Scheduler.

这可以使用 Windows 任务计划程序轻松手动完成。

  • First, install forever.
  • Then, create a batch file that contains the following:

    cd C:\path\to\project\root
    call C:\Users\Username\AppData\Roaming\npm\forever.cmd start server.js
    exit 0
    
  • Lastly, create a scheduled task that runs when you log on. This task should call the batch file.

  • 首先,永远安装。
  • 然后,创建一个包含以下内容的批处理文件:

    cd C:\path\to\project\root
    call C:\Users\Username\AppData\Roaming\npm\forever.cmd start server.js
    exit 0
    
  • 最后,创建一个在您登录时运行的计划任务。此任务应调用批处理文件。

回答by MichaelMilom

I would recommend installing your node.js app as a Windows service, and then set the service to run at startup. That should make it a bit easier to control the startup action by using the Windows Services snapin rather than having to add or remove batch files in the Startup folder.

我建议将您的 node.js 应用程序安装为 Windows 服务,然后将该服务设置为在启动时运行。通过使用 Windows 服务管理单元来控制启动操作应该更容易一些,而不必在启动文件夹中添加或删除批处理文件。

Another service-related question in Stackoverflow provided a couple of (apprently) really good options. Check out How to install node.js as a Windows Service. node-windowslooks really promising to me. As an aside, I used similar tools for Java apps that needed to run as services. It made my life a whole lot easier. Hope this helps.

Stackoverflow 中的另一个与服务相关的问题提供了几个(显然)非常好的选择。查看如何将 node.js 安装为 Windows 服务node-windows对我来说看起来很有希望。顺便说一句,我对需要作为服务运行的 Java 应用程序使用了类似的工具。它让我的生活变得更加轻松。希望这可以帮助。

回答by alicanozkara

you should try this

你应该试试这个

npm forever

永远的 npm

https://www.npmjs.com/package/forever

https://www.npmjs.com/package/forever

回答by steampowered

Use pm2to start and run your nodejs processes on windows.

使用PM2启动和在Windows上运行的进程的NodeJS。

Be sure to read this github discussion of how to set up task scheduler to start pm2: https://github.com/Unitech/pm2/issues/1079

请务必阅读有关如何设置任务调度程序以启动 pm2 的 github 讨论:https: //github.com/Unitech/pm2/issues/1079

回答by Yar

Here is another solutionI wrote in C# to auto startup native node server or pm2 server on Windows.

这是我用 C# 编写的另一个解决方案,用于在 Windows 上自动启动本机节点服务器或 pm2 服务器。

回答by Sachin Hayatnagarkar

I know there are multiple ways to achieve this as per solutions shared above. I haven't tried all of them but some third party services lack clarity around what are all tasks being run in the background. I have achieved this through a powershell script similar to the one mentioned as windows batch file. I have scheduled it using Windows Tasks Scheduler to run every minute. This has been quite efficient and transparent so far. The advantage I have here is that I am checking the process explicitly before starting it again. This wouldn't cause much overhead to the CPU on the server. Also you don't have to explicitly place the file into the startup folders.

我知道根据上面共享的解决方案,有多种方法可以实现这一目标。我还没有尝试过所有这些,但一些第三方服务不清楚在后台运行的所有任务是什么。我通过一个类似于 Windows 批处理文件中提到的 powershell 脚本实现了这一点。我已经使用 Windows 任务计划程序安排它每分钟运行一次。到目前为止,这是非常有效和透明的。我在这里的优势是我在再次启动之前明确检查了流程。这不会对服务器上的 CPU 造成太多开销。此外,您不必明确地将文件放入启动文件夹中。

function CheckNodeService ()
{

$node = Get-Process node -ErrorAction SilentlyContinue

if($node)
{
    echo 'Node Running'
}
else
{
    echo 'Node not Running'
    Start-Process "C:\Program Files\nodejs\node.exe" -ArgumentList "app.js" -WorkingDirectory "E:\MyApplication"
    echo 'Node started'

}
}

CheckNodeService

回答by clay

Copied directly from this answer:

直接从这个答案复制:

You could write a script in any language you want to automate this (even using nodejs) and then just install a shortcut to that script in the user's %appdata%\Microsoft\Windows\Start Menu\Programs\Startup folder

你可以用任何你想要自动化的语言编写脚本(甚至使用 nodejs),然后只需在用户的 %appdata%\Microsoft\Windows\Start Menu\Programs\Startup 文件夹中安装该脚本的快捷方式