Files
hometown/start-api.sh
2026-03-08 17:37:02 +08:00

33 lines
1.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 仅启动后端 API前后端分离时使用
# 在项目根目录执行: ./start-api.sh 或 PORT=3001 ./start-api.sh
# 若修改了默认端口,前端 api.config.json 的 apiBase 需填完整地址(含端口),如 http://域名或IP:5599
set -e
cd "$(dirname "$0")/server"
PORT="${PORT:-5599}"
export PORT
# 释放端口,避免 EADDRINUSE上次未退出或多次启动
if command -v lsof >/dev/null 2>&1; then
pids=$(lsof -t -i ":$PORT" 2>/dev/null) || true
if [ -n "$pids" ]; then
echo "[720yun-offline-api] 停止占用端口 ${PORT} 的进程: $pids"
echo "$pids" | xargs kill 2>/dev/null || true
sleep 1
fi
elif command -v fuser >/dev/null 2>&1; then
if fuser -n tcp "$PORT" >/dev/null 2>&1; then
echo "[720yun-offline-api] 停止占用端口 ${PORT} 的进程..."
fuser -k "$PORT/tcp" 2>/dev/null || true
sleep 1
fi
fi
echo "[720yun-offline-api] 安装依赖..."
npm install --no-audit --no-fund
echo "[720yun-offline-api] 启动 API http://0.0.0.0:${PORT}"
echo "[720yun-offline-api] 前端请将 api.config.json 的 apiBase 设为: http://本机IP或域名:${PORT}"
exec node index.js