Skip to content
This repository has been archived by the owner on Dec 4, 2022. It is now read-only.

Allow use of dynamic ports? #12

Open
OmgImAlexis opened this issue Nov 22, 2022 · 3 comments
Open

Allow use of dynamic ports? #12

OmgImAlexis opened this issue Nov 22, 2022 · 3 comments

Comments

@OmgImAlexis
Copy link

I was hoping something like this could be added?

import KingWorld from 'kingworld'

const server = new KingWorld()
	.get('/', () => 'Hello KingWorld')
	.listen(0)
	 
console.log('🦊 KINGWORLD is running at :%s', server.port)

This would also help in this case.

import KingWorld from 'kingworld'

const server = new KingWorld()
	.get('/', () => 'Hello KingWorld')
	.listen(process.env.PORT ?? 0)

console.log('🦊 KINGWORLD is running at :%s', server.port)
@SaltyAom
Copy link
Owner

SaltyAom commented Nov 22, 2022

Ok, that's a nice feature that I completely forgot about.

TLDR; Update to 0.0.0-experimental.52

I was going to provide the reply with something like "leaving it to dev to handle numeric string" but figured out that it required code more than 1 line, and realized that framework should handle that kind of stuff, which looks something like this:

let port = +process.env.PORT
if(Number.isNan(port)) port = 3000

new KingWorld().listen(port)

So the fix is released on exp.52 (see change here: #7680bdf, it should now accept stringified numeric value, so you can pass from process.env.PORT like syntax you proposed above.

Also, I believe I didn't write this down on documentation yet, but to access the port you can either:

  1. access .server property which is instance of Bun.server to access port and hostname.
  2. callback function as 2nd parameter on .listen like express/fastify

So basically:

const app = new KingWorld()
    // listen can accept 2nd parameter callback for accessing `Bun.serve`
    .listen(process.env.PORT ?? 8080, ({ hostname, port }) => {
        console.log(
            `🦊 KingWorld running at http://${hostname}:${port}`
        )
    })

// Or access it via `.server`, both are the same, as `.listen` is sync.
console.log(
    `🦊 KingWorld running at http://${app.server?.hostname}:${app.server?.port}`
)

Also, I believe I saw your profile picture on Bun Discord, if so feel free to tag me there if you find any issues.

Let me know if you need anything, or else if you find this helpful, you can close the issue.

@OmgImAlexis
Copy link
Author

Perfect, that fixes being able to pass in process.env.PORT. 😄

Still have 2 issues in regards to ports though.

  1. It doesn't allow port 0. This is used when wanting a random port to be assigned.
  2. Unix socket paths aren't allowed (I don't think bun has support for them yet though).

@OmgImAlexis
Copy link
Author

Looks like the port 0 issue is related to bun itself not this lib oven-sh/bun#1544.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants