programing

node.js를 Windows 서비스로 설치하는 방법은 무엇입니까?

sourcejob 2023. 8. 12. 10:13
반응형

node.js를 Windows 서비스로 설치하는 방법은 무엇입니까?

node.js 실행 파일을 다운로드했습니다.Windows 서비스로 해당 실행 파일을 실행하려면 어떻게 해야 합니까?여러 버전의 node.js를 동시에 실행해야 하므로 표준 node.js 설치 관리자를 사용할 수 없습니다.

파티에 늦었지만 노드 윈도우도 효과를 볼 것입니다.

enter image description here

또한 시스템 로깅도 내장되어 있습니다.

enter image description here

코드에서 스크립트를 생성하는 API가 있습니다.

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();

FD: 저는 이 모듈의 저자입니다.

저는 그것이 매우 유용하다는 것을 알게 되어 그것 주변에 훨씬 더 사용하기 쉬운 포장지(npm, github)를 만들었습니다.

설치:

npm install -g qckwinsvc

서비스 설치:

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

서비스 제거:

qckwinsvc --vmdk

prompt: Service name: [name of your service]
prompt: Node script path: [path of your node script]
Service stopped
Service uninstalled

WinSer는 인기 있는 NSSM(Non-Sucking Service Manager) 주변의 node.js 친화적인 래퍼입니다.

이 블로그에서

다음으로, 저는 IIS처럼 노드를 서비스로 호스팅하고 싶었습니다.이렇게 하면 기계에서 시작하여 백그라운드에서 실행되고 충돌 시 자동으로 다시 시작됩니다.

여기서 비흡입 서비스 관리자인 nssm이 사진에 들어갑니다.이 도구를 사용하면 일반 .exe를 윈도우즈 서비스로 호스팅할 수 있습니다.

다음은 노드 애플리케이션의 인스턴스를 서비스로 설정하고 관리자처럼 cmd를 열고 다음 명령을 입력하는 데 사용한 명령입니다.

nssm.exe install service_name c:\your_nodejs_directory\node.exe c:\your_application_directory\server.js
net start service_name

직접적으로 질문을 하는 것이 아니라 보다 node.js 방식으로 귀하의 요구 사항을 충족할 수 있는 대안을 제공하는 것입니다.

기능적으로 요구 사항은 다음과 같습니다.

  1. 백그라운드에서 논리(앱)가 실행되도록 합니다.
  2. 논리를 시작/정지할 수 있음
  3. 시스템 부팅 시 논리 자동 시작

프로세스 관리자(PM)를 사용하고 시스템 시작 시 프로세스 관리자를 시작하면 이러한 요구 사항을 충족할 수 있습니다.Windows 친화적인 두 가지 좋은 PM은 다음과 같습니다.

PM이 자동으로 시작되도록 하려면 가장 간단한 방법은 "시작 시" 트리거를 사용하여 예약된 작업을 만드는 것입니다.

enter image description here

qckwinsvc가 한동안 업데이트되지 않았기 때문에 qckwinsvc2(npm, github)라는 새로운 버전이 있습니다.

이제 서비스에 전달된 인수를 지원합니다.또한 로컬 캐시를 유지하므로 작업을 수행할 때마다 경로를 제공할 필요가 없습니다.

설치 즉시 서비스를 시작하려면 nowarg를 사용합니다.

qckwinsvc2 install name="Hello" description="Hello World" path="C:\index.js" args="--y" now
qckwinsvc2 uninstall name="Hello"
qckwinsvc2 list

1년 전에 게시한 프로세스 관리자 + 작업 스케줄러 접근 방식은 일부 일회성 서비스 설치에서 잘 작동합니다.하지만 최근에 저는 많은 작은 서비스들이 IPC를 통해 서로 대화하는 마이크로 서비스 방식으로 시스템을 설계하기 시작했습니다.따라서 각 서비스를 수동으로 구성하는 것은 더 이상 참을 수 없습니다.

수동 구성 없이 서비스를 설치한다는 목표를 위해 명령줄 도구인 serman을 만들었습니다.npm i -g serman실행 파일을 서비스로 설치합니다.한 번만 작성하면 되는 간단한 서비스 구성 파일과 실행 파일만 있으면 됩니다. 실행

serman install <path_to_config_file>

서비스를 설치합니다.stdout그리고.stderr모두 기록됩니다.자세한 내용은 프로젝트 웹 사이트를 참조하십시오.

아래에 나와 있는 것처럼 작업 구성 파일은 매우 간단합니다.하지만 그것은 또한 다음과 같은 많은 유용한 기능들을 가지고 있습니다.<env>그리고.<persistent_env>

<service>
  <id>hello</id>
  <name>hello</name>
  <description>This service runs the hello application</description>

  <executable>node.exe</executable>

  <!-- 
       {{dir}} will be expanded to the containing directory of your 
       config file, which is normally where your executable locates 
   -->
  <arguments>"{{dir}}\hello.js"</arguments>

  <logmode>rotate</logmode>

  <!-- OPTIONAL FEATURE:
       NODE_ENV=production will be an environment variable 
       available to your application, but not visible outside 
       of your application
   -->
  <env name="NODE_ENV" value="production"/>

  <!-- OPTIONAL FEATURE:
       FOO_SERVICE_PORT=8989 will be persisted as an environment
       variable machine-wide.
   -->
  <persistent_env name="FOO_SERVICE_PORT" value="8989" />
</service>

https://nssm.cc/ 서비스 도우미는 nssm의 배치 파일로 윈도우 서비스를 만들기에 좋습니다. 저는 어떤 앱이든 어떤 파일이든 잘 작동합니다.

언급URL : https://stackoverflow.com/questions/10547974/how-to-install-node-js-as-windows-service

반응형