3v324v23's picture
fixbug:增强日志
042a329
Raw
History Blame Contribute Delete
1.28 kB
/**
* 本地开发启动入口。
*/
import app from './app.js'
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const PORT = Number(process.env.PORT || 3210)
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const projectRootDir = path.resolve(__dirname, '..', '..')
const distDir = path.resolve(projectRootDir, 'dist')
process.on('unhandledRejection', (reason) => {
console.error('未处理的 Promise 拒绝:', reason)
})
process.on('uncaughtException', (error) => {
console.error('未捕获的异常:', error)
process.exit(1)
})
const server = app.listen(PORT, '0.0.0.0', () => {
console.log(
`[boot] NODE_ENV=${process.env.NODE_ENV || ''} PORT=${PORT} cwd=${process.cwd()} root=${projectRootDir} distDir=${distDir} distExists=${fs.existsSync(distDir)} DATA_DIR=${process.env.DATA_DIR || ''}`,
)
console.log(`API 服务已启动:http://0.0.0.0:${PORT}`)
})
function shutdown(signal: string) {
console.log(`${signal} 信号已接收,正在关闭服务...`)
server.close(() => {
console.log('服务已关闭')
process.exit(0)
})
}
process.on('SIGTERM', () => shutdown('SIGTERM'))
process.on('SIGINT', () => shutdown('SIGINT'))
export default app