如何完全自动化节点js服务器开/关过程

How to completely automate the node js server switching on/off process

本文关键字:过程 服务器 js 何完全 自动化 节点      更新时间:2023-09-26

我开发了一个使用express js的应用程序。为了自动化该过程,我创建了一个 jenkins 作业,使用的命令如下

ssh "ip address of the server" node app.js "a" "b" "c" "d" "e" &
curl "url of the application"
curl "another url of the application"

这里 a,b,c,d,e 是我在启动服务器时传递给我的应用程序的命令行参数。问题是每次我都必须先关闭服务器然后运行作业。整个过程如何自动化,服务器应该继续在后台运行。如果有人想对所提出的问题进行更多澄清,请告诉我。任何帮助都深表感谢。

步骤1 通过以下方式摆脱传递的参数:

选项 1

package.json中使用npm脚本与forever

"scripts": {
    "start": "forever express/app.js a b c"
  },

运行服务器npm start的命令

选项 2

使用batsh文件重启应用

文件.bat内容

forever express/app.js a b c

自动化.js

var automaton = require('automaton').create();
automaton.run('run', {cmd: 'file.bat'}).pipe(process.stdout);

用于运行服务器node automate的命令


步骤2将永久代码配置为在package.jsonfile.bat中发现某些更改时分别运行npm startnode automate

查看它的实际效果,克隆 Git 存储库并运行 node express'index.js 或运行npm start

祝您乐于助人!