()
| 72 | } |
| 73 | |
| 74 | public start(): boolean { |
| 75 | if (this.server) { |
| 76 | this.stop() |
| 77 | } |
| 78 | |
| 79 | try { |
| 80 | this.server = createServer((req, res) => this.handleRequest(req, res)) |
| 81 | |
| 82 | this.server.on('error', (error: NodeJS.ErrnoException) => { |
| 83 | console.error('[HttpServer] 服务器错误:', error) |
| 84 | if (error.code === 'EADDRINUSE') { |
| 85 | console.error(`[HttpServer] 端口 ${this.config.port} 已被占用`) |
| 86 | } |
| 87 | this.server = null |
| 88 | }) |
| 89 | |
| 90 | this.server.listen(this.config.port, '127.0.0.1', () => { |
| 91 | console.log(`[HttpServer] 服务已启动: http://127.0.0.1:${this.config.port}`) |
| 92 | }) |
| 93 | |
| 94 | return true |
| 95 | } catch (error) { |
| 96 | console.error('[HttpServer] 启动失败:', error) |
| 97 | this.server = null |
| 98 | return false |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public stop(): void { |
| 103 | if (this.server) { |
no test coverage detected