首先需要搞清楚express node项目在本地是怎么部署启动的、然后在根据如下步骤编写bat脚本:
思路
1.检查项目是否已启动
2.通过pm2启动项目
bat脚本
@echo off
echo windows10 x64 project-api-start
REM 请求管理员权限并隐藏窗口
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 :: & echo 执行中,请稍候... & timeout 2 > nul","","runas",0)(window.close)&&exit
setlocal enabledelayedexpansionrem 设置 Mysql 目录变量,方便后续引用和修改
%~d0
cd %~dp0
set CURRENT_DIR=%~dp0
set CURRENT_DIR=%CURRENT_DIR:~0,-1%
set PM2_PATH=%CURRENT_DIR%\node-v16.14.2\node_global\pm2rem 定义函数来启动单个项目
:startProject
setlocal
set "projectPath=%~1"
set "projectName=%~2"if not "%projectPath%"=="" if not "%projectName%"=="" (pushd %projectPath%rem 检查项目是否已在运行for /f "delims=" %%i in ('%PM2_PATH% list ^| find /i "%projectName%"') do set "projectStatus=%%i"if not defined projectStatus (%PM2_PATH% start "%projectPath%\bin\www" --name="%projectName%"echo Project %projectName% started) else (echo Project %projectName% is already running)popd
)rem 启动项目 demo-api-3001
echo Started-Project demo-api-3001
call :startProject "%CURRENT_DIR%\demo-api-3001" "demo-api-3001"rem 启动项目 demo-api-3002
echo Started-Project demo-api-3002
call :startProject "%CURRENT_DIR%\demo-api-3002" "demo-api-3002"echo project-api-start success
endlocalREM 执行完成后自动关闭
timeout /t 2 >nul
exit
node后端项目:
demo-api-3001
demo-api-3002
亲测有效。