From b3f8a5b5041b7d6ee157fb92033d5a3b5a97821d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=A3?= <2943102883@qq.com> Date: Sat, 13 Jul 2024 13:24:16 +0800 Subject: [PATCH] Port can be dynamically configured --- example/port.ts | 13 +++++++++++++ src/index.ts | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 example/port.ts diff --git a/example/port.ts b/example/port.ts new file mode 100644 index 00000000..ed152fdb --- /dev/null +++ b/example/port.ts @@ -0,0 +1,13 @@ +import { Elysia } from '../src' + +new Elysia() + .decorate('port', 9876) + .get('/', () => { + return 'hello' + }) + .listen( + ({ port }) => port, + ({ hostname, port }) => { + console.log(`🦊 running at http://${hostname}:${port}`) + } + ) diff --git a/src/index.ts b/src/index.ts index 444902d8..07e265ec 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5227,7 +5227,7 @@ export default class Elysia< * ``` */ listen = ( - options: string | number | Partial, + options: string | number | ((data: Singleton['decorator']) => number | string) | Partial, callback?: ListenCallback ) => { if (typeof Bun === 'undefined') @@ -5243,6 +5243,9 @@ export default class Elysia< options = parseInt(options) } + if (typeof options === 'function') { + options = options(this.decorator) + } const fetch = this.fetch