Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting Elysia from file causes server to run twice #550

Closed
EvHaus opened this issue Mar 18, 2024 · 5 comments
Closed

Exporting Elysia from file causes server to run twice #550

EvHaus opened this issue Mar 18, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@EvHaus
Copy link

EvHaus commented Mar 18, 2024

What version of Elysia.JS is running?

1.0.0

What platform is your computer?

Linux 5.15.133.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

Create a basic Elysia server like this:

import { Elysia } from 'elysia';
const app = new Elysia()
  .get('/', () => 'Landing')
  .listen(8080)

console.log(
	`🦊 API is running at ${app.server?.hostname}:${app.server?.port}`,
);

export default app;

Then run the server via bun index.ts.

What is the expected behavior?

Server should run with no errors.

What do you see instead?

Server crashes with:

bun run index.ts
 6 |       if(typeof entryNamespace?.default?.fetch === 'function')  {
 7 |         Bun.serve(entryNamespace.default);
 8 |       }
 9 |    }, reportError);
10 | } else if (typeof entryNamespace?.default?.fetch === 'function') {
11 |    Bun.serve(entryNamespace.default);
     ^
EADDRINUSE: Failed to start server. Is port 8080 in use?
 syscall: "listen"

      at bun:main:11:4

The problem seems to be that doing export default app in Elysia 1.0.0 causes a separate listen call to be executed.

Removing export default app makes the issue go away, but this means you can't import the file for the purpose of unit testing as shown here.

Removing listen(8080) also makes the issue go away, but causes app.server not to be defined, so the console message prints: API is running at undefined:undefined instead.

I can also remove export default app and change const app = ... to export const app = ... to fix this, and I suppose this is what you recommend to do, but this behavior was not expected (or documented) so I thought I'd log it.

Additional information

No response

@EvHaus EvHaus added the bug Something isn't working label Mar 18, 2024
@EvHaus EvHaus changed the title Exporting Elysia from causes server to run twice Exporting Elysia from file causes server to run twice Mar 18, 2024
@DobroslavR
Copy link

Why are you exporting it? If you need only type, you can do export type API = typeof app and it's gonna work.

@EvHaus
Copy link
Author

EvHaus commented Apr 24, 2024

I'm exporting it so I can pass it to treaty during tests.

import app from './';
import {treaty} from '@elysiajs/eden';

const api = treaty(app);

@kravetsone
Copy link
Contributor

It's bun behaviour

https://bun.sh/docs/api/http#object-syntax

@SaltyAom
Copy link
Member

Close as not going to fix as it's Bun behavior, and changing this behavior would introduce large breaking change.

@SaltyAom SaltyAom closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2024
EvHaus added a commit to EvHaus/documentation that referenced this issue Aug 31, 2024
@EvHaus
Copy link
Author

EvHaus commented Aug 31, 2024

I put up a small PR to update the docs around this, which I hope will help those who are new to Elysia. I hope you'll consider adding it. elysiajs/documentation#384

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants