|
1 | 1 | import { echo } from 'coa-echo' |
2 | 2 | import { _ } from 'coa-helper' |
3 | | -import { createServer, IncomingMessage, ServerResponse } from 'http' |
| 3 | +import { createServer, IncomingMessage, Server, ServerResponse } from 'http' |
4 | 4 | import { CoaRequestBody } from '../base/CoaRequestBody' |
5 | 5 | import { CoaContext, CoaContextConstructor } from './CoaContext' |
6 | 6 | import { CoaRouter } from './CoaRouter' |
7 | 7 |
|
8 | 8 | export class CoaApplication<T extends CoaContext> { |
9 | | - private readonly router: CoaRouter<T> |
| 9 | + public readonly server: Server |
| 10 | + public readonly router: CoaRouter<T> |
10 | 11 | private readonly Context: CoaContextConstructor<T> |
11 | 12 | private readonly startAt: bigint = process.hrtime.bigint() |
12 | 13 |
|
13 | 14 | constructor(Context: CoaContextConstructor<T>, router: CoaRouter<T>) { |
| 15 | + echo.info('[server] Booting...') |
14 | 16 | this.Context = Context |
15 | 17 | this.router = router |
16 | | - echo.info('[server] Booting...') |
| 18 | + this.server = createServer((req, res) => { |
| 19 | + this.requestListener(req, res).catch((e) => echo.error(e)) |
| 20 | + }) |
17 | 21 | } |
18 | 22 |
|
19 | 23 | async start(entry: string = '') { |
20 | 24 | // 设置端口 |
21 | 25 | const port = parseInt(process.env.HOST || '') || 8000 |
22 | 26 |
|
23 | 27 | // 启动服务 |
24 | | - const server = createServer( |
25 | | - async (req, res) => |
26 | | - await this.requestListener(req, res).catch((e) => { |
27 | | - echo.error(e) |
28 | | - }) |
29 | | - ) |
30 | | - server.listen(port, () => { |
| 28 | + this.server.listen(port, () => { |
31 | 29 | echo.info(`[server] Startup successful in: ${Number(process.hrtime.bigint() - this.startAt) / 1e6} ms`) |
32 | 30 | echo.info(`[server] Listening on: http://localhost:${port}${entry}`) |
33 | 31 | }) |
|
0 commit comments