Config
You can customize Elysia behavior by:
- using constructor
- using
listen
Constructor
Constructor will change some behavior of Elysia.
typescript
new Elysia({
serve: {
hostname: '0.0.0.0'
}
})
new Elysia({
serve: {
hostname: '0.0.0.0'
}
})
Listen
.listen
will config any value for starting server.
By default listen
will either accept number
or Object
.
For Object, listen
accept the same value as Bun.serve
, you can provide any custom one except serve
.
typescript
// ✅ This is fine
new Elysia()
.listen(8080)
// ✅ This is fine
new Elysia()
.listen({
port: 8080,
hostname: '0.0.0.0'
})
// ✅ This is fine
new Elysia()
.listen(8080)
// ✅ This is fine
new Elysia()
.listen({
port: 8080,
hostname: '0.0.0.0'
})
TIP
For providing WebSocket, please use @elysiajs/websocket
Custom Port
You can provide a custom port from ENV by using p
typescript
new Elysia()
.listen(process.env. PORT ?? 8080)
new Elysia()
.listen(process.env. PORT ?? 8080)
Retrieve Port
You can get underlying Server
instance from either using: .server
property. Using callback in .listen
typescript
const app = new Elysia()
.listen(8080, ({ hostname, port }) => {
console.log(`Running at http://${hostname}:${port}`)
})
// `server` will be null if listen isn't called
console.log(`Running at http://${app.server!.hostname}:${app.server!.port}`)
const app = new Elysia()
.listen(8080, ({ hostname, port }) => {
console.log(`Running at http://${hostname}:${port}`)
})
// `server` will be null if listen isn't called
console.log(`Running at http://${app.server!.hostname}:${app.server!.port}`)