简体   繁体   English

作为 Windows 服务启动 nestjs 项目

[英]Start nestjs project as a Windows service

We used to start Angular and NestJS (based on node.js) projects using Docker containers.我们曾经使用 Docker 容器启动 Angular 和 NestJS(基于 node.js)项目。 This solution was discontinued for various reasons, so we are looking for a way to start these projects at the start of the PC (or on a trigger) and restart the project automatically if a crash occurs.由于各种原因,该解决方案已停止使用,因此我们正在寻找一种方法来在 PC 启动时(或在触发器上)启动这些项目,并在发生崩溃时自动重新启动项目。

node-windows节点窗口

This package builds a Windows service from a node.js project.这个 package 从 node.js 项目构建了一个 Windows 服务。 NestJS being based on node.js, starting it using node.js is done this way (while in the project's folder): NestJS 基于 node.js,使用 node.js 启动它是这样完成的(在项目文件夹中):

node PATH_TO_PROJECT\node_modules\@nestjs\cli\bin\nest.js start --config.\tsconfig.build.json

The script used:使用的脚本:

const svc = new Service({
  name: 'Test',
  description: 'Test',
    script:
      'PATH_TO_PROJECT\\node_modules\\@nestjs\\cli\\bin\\nest.js',
    scriptOptions: [
      'start --watch --config PATH_TO_PROJECT\\tsconfig.build.json',
    ],
  ],
  execPath: 'C:\\Program Files\\nodejs\\node.exe',
});

svc.on('install', function () {
  console.log('installed');
  svc.start();
});
svc.install();

The installation works as intended but in a browser, the server cannot be reached.安装按预期工作,但在浏览器中无法访问服务器。

Questions问题

  1. Is there a way to use node-windows for a NestJS project?有没有办法在 NestJS 项目中使用节点窗口?
  2. Is it possible to use an absolute path with the nest cli start command? nest cli start命令是否可以使用绝对路径? (eg nest start --config ABSOLUTE_PATH) (例如 nest start --config ABSOLUTE_PATH)
  3. How would you start an Angular project the same way?你将如何以同样的方式启动一个 Angular 项目?

Thank you.谢谢你。

am use 'child_process' lib for run command like this我使用'child_process'库来运行这样的命令

server.js服务器.js

const { exec } = require("child_process");

exec("npm run start", (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});

and use node-windows like this并像这样使用节点窗口

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

console.log(serviceDetail);
// Create a new service object
var svc = new Service({
  name: serviceDetail.name,
  description: serviceDetail.detail,
  script: './server.js'
});
console.log('start building up service name ' + serviceDetail.name);
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});

svc.install();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM