Auto start node.js server on boot

后端 未结 10 1326
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 17:16

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

10条回答
  •  有刺的猬
    2020-11-30 17:41

    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.

    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
    

提交回复
热议问题