Skip to content

Commit 9830a5c

Browse files
committed
Add server export
1 parent 5823530 commit 9830a5c

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

.devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"name": "coajs",
55
"image": "mcr.microsoft.com/vscode/devcontainers/javascript-node",
66
"postCreateCommand": "yarn",
7-
"extensions": ["mutantdino.resourcemonitor", "esbenp.prettier-vscode", "dbaeumer.vscode-eslint", "eamodio.gitlens"]
7+
"extensions": ["mutantdino.resourcemonitor", "esbenp.prettier-vscode", "dbaeumer.vscode-eslint"]
88
}

lib/service/CoaApplication.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
import { echo } from 'coa-echo'
22
import { _ } from 'coa-helper'
3-
import { createServer, IncomingMessage, ServerResponse } from 'http'
3+
import { createServer, IncomingMessage, Server, ServerResponse } from 'http'
44
import { CoaRequestBody } from '../base/CoaRequestBody'
55
import { CoaContext, CoaContextConstructor } from './CoaContext'
66
import { CoaRouter } from './CoaRouter'
77

88
export class CoaApplication<T extends CoaContext> {
9-
private readonly router: CoaRouter<T>
9+
public readonly server: Server
10+
public readonly router: CoaRouter<T>
1011
private readonly Context: CoaContextConstructor<T>
1112
private readonly startAt: bigint = process.hrtime.bigint()
1213

1314
constructor(Context: CoaContextConstructor<T>, router: CoaRouter<T>) {
15+
echo.info('[server] Booting...')
1416
this.Context = Context
1517
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+
})
1721
}
1822

1923
async start(entry: string = '') {
2024
// 设置端口
2125
const port = parseInt(process.env.HOST || '') || 8000
2226

2327
// 启动服务
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, () => {
3129
echo.info(`[server] Startup successful in: ${Number(process.hrtime.bigint() - this.startAt) / 1e6} ms`)
3230
echo.info(`[server] Listening on: http://localhost:${port}${entry}`)
3331
})

lib/service/CoaHttp.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export class CoaHttp<T extends CoaContext> {
3232

3333
// 启动应用
3434
await this.application.start(this.config.baseUrl + 'doc')
35+
36+
return { server: this.application.server }
3537
}
3638

3739
// 注册路由

0 commit comments

Comments
 (0)