From 242d079994ab103bc4cfa1b207b68aa04b56ff98 Mon Sep 17 00:00:00 2001 From: Neco86 <33189641+Neco86@users.noreply.github.com> Date: Thu, 11 May 2023 19:28:15 +0800 Subject: [PATCH] close server when exit Handle the SIGINT signal (such as when the user presses Ctrl+C in the terminal) by first closing the server and then exiting the program. --- index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/index.ts b/index.ts index 98a90fc..e317d4c 100644 --- a/index.ts +++ b/index.ts @@ -60,6 +60,12 @@ router.get('/ask/stream', async (ctx) => { app.use(router.routes()); -app.listen(3000, () => { +const server = app.listen(3000, () => { console.log("Now listening: 127.0.0.1:3000"); }); + +process.on('SIGINT', () => { + server.close(() => { + process.exit(0); + }); +});