diff --git a/.gitignore b/.gitignore index 242283e..c8686f0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ node_modules/ dist/ data/ server/data/ +logs/ diff --git a/check-service.sh b/check-service.sh new file mode 100644 index 0000000..9e54eb1 --- /dev/null +++ b/check-service.sh @@ -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 diff --git a/docs/nginx-api反向代理.conf b/docs/nginx-api反向代理.conf new file mode 100644 index 0000000..47f0ddb --- /dev/null +++ b/docs/nginx-api反向代理.conf @@ -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; +} diff --git a/run.sh b/run.sh index 05a539d..c0a7bb3 100644 --- a/run.sh +++ b/run.sh @@ -1,12 +1,14 @@ #!/usr/bin/env bash -# 部署流程:拉代码 → 安装依赖 → 构建前端到 dist/ → 启动后端 API(5599) +# 部署流程:拉代码 → 安装依赖 → 构建前端到 dist/ → 后台启动后端 API(5599) # 若 Nginx 未反代 /api,请先设置 API_BASE_URL 再执行,例如: # export API_BASE_URL="http://你的域名或IP:5599" # ./run.sh -# 若 Nginx 已将 /api 反代到本机 5599,可留空 api.config.json(apiBase 为空即可)。 +# 使用 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 \ No newline at end of file + +# 后台启动 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] 前台启动 API(Ctrl+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" \ No newline at end of file