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