fix: 优化启动脚本

This commit is contained in:
Daniel
2026-03-08 00:03:14 +08:00
parent 7db497bc94
commit 321fad8a0d
4 changed files with 64 additions and 3 deletions

1
.gitignore vendored
View File

@@ -10,3 +10,4 @@ node_modules/
dist/
data/
server/data/
logs/

29
check-service.sh Normal file
View File

@@ -0,0 +1,29 @@
#!/usr/bin/env bash
# 检查本项目的 Node 服务是否启动(默认 API 端口 5599
# 用法: ./check-service.sh [端口],不传端口则用 5599
PORT="${1:-5599}"
echo "检查端口 ${PORT} 是否在监听..."
if command -v ss >/dev/null 2>&1; then
ss -tlnp | grep -E ":$PORT\s" || true
elif command -v netstat >/dev/null 2>&1; then
netstat -tlnp 2>/dev/null | grep -E ":$PORT\s" || true
fi
if ! ss -tlnp 2>/dev/null | grep -q ":$PORT "; then
if ! netstat -tlnp 2>/dev/null | grep -q ":$PORT "; then
echo "端口 ${PORT} 未在监听,服务可能未启动。"
echo "启动方式: ./start-api.sh 或 ./run.sh"
exit 1
fi
fi
echo "--- 测试 API ---"
if command -v curl >/dev/null 2>&1; then
resp=$(curl -s -o /dev/null -w "%{http_code}" "http://127.0.0.1:${PORT}/api/config" 2>/dev/null)
if [ "$resp" = "200" ]; then
echo "GET /api/config 返回 200服务正常。"
else
echo "GET /api/config 返回 ${resp},请检查服务。"
fi
else
echo "未安装 curl仅检查了端口监听。"
fi

View File

@@ -0,0 +1,11 @@
# 将 /api 请求转发到本机 Node 后端(端口与 start-api.sh 一致,默认 5599
# 把下面这段加入 server { ... } 内,建议放在 root 指令之后、其他 location 之前
location /api {
proxy_pass http://127.0.0.1:5599;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

26
run.sh
View File

@@ -1,12 +1,14 @@
#!/usr/bin/env bash
# 部署流程:拉代码 → 安装依赖 → 构建前端到 dist/ → 启动后端 API5599
# 部署流程:拉代码 → 安装依赖 → 构建前端到 dist/ → 后台启动后端 API5599
# 若 Nginx 未反代 /api请先设置 API_BASE_URL 再执行,例如:
# export API_BASE_URL="http://你的域名或IP:5599"
# ./run.sh
# 若 Nginx 已将 /api 反代到本机 5599可留空 api.config.jsonapiBase 为空即可)。
# 使用 pm2 保活推荐export USE_PM2=1 && ./run.sh
set -e
cd "$(dirname "$0")"
API_PORT="${PORT:-5599}"
export PORT="$API_PORT"
git fetch origin && git reset --hard origin/master
npm install --no-audit --no-fund
@@ -18,4 +20,22 @@ if [ -n "$API_BASE_URL" ]; then
fi
node build.js
bash start-api.sh
# 后台启动 API优先 pm2否则 nohup
if [ -n "$USE_PM2" ] && command -v pm2 >/dev/null 2>&1; then
echo "[run.sh] 使用 pm2 启动 API端口 ${API_PORT}..."
(cd server && npm install --no-audit --no-fund --silent && pm2 delete view-airtep-api 2>/dev/null; true)
(cd server && pm2 start index.js --name view-airtep-api)
pm2 save 2>/dev/null || true
echo "[run.sh] API 已由 pm2 托管。查看: pm2 list / pm2 logs view-airtep-api"
elif command -v nohup >/dev/null 2>&1; then
echo "[run.sh] 使用 nohup 后台启动 API端口 ${API_PORT}..."
mkdir -p logs
nohup bash start-api.sh >> logs/api.log 2>&1 &
echo "[run.sh] API 已在后台运行。查看日志: tail -f logs/api.log"
else
echo "[run.sh] 前台启动 APICtrl+C 会停止服务)..."
exec bash start-api.sh
fi
echo "[run.sh] 部署完成。前端静态在 dist/API 端口 ${API_PORT}。检查: curl -s http://127.0.0.1:${API_PORT}/api/config"