Application & server
The Lykos class is the application entry point. It manages route registration, middleware, dependency injection, Socket.IO integration and HTTP server startup.
Application instance
Every Lykos application starts with a new instance of the Lykos class. The instance owns its router, middleware registry, dependency injection container and module loader.
1import { Lykos } from 'lykosjs'2 3const app = new Lykos()Create the application once and use the same instance to register routes, configure middleware, load modules and start the HTTP server.
Register global middleware
Use app.use() to register middleware that should run for application routes. Global middleware is added to the middleware pipeline when a route is registered.
1app.use(async (ctx, reply, next) => {2 console.log(`[Request] ${ctx.method} ${ctx.path}`)3 return next()4})Register routes
The application exposes methods for registering HTTP routes. Each method accepts a path, a handler and optional route-specific middleware.
1app.GET('/', async (ctx, reply) => {2 return reply.OK({3 message: 'Welcome to LykosJS'4 })5})6 7app.POST('/users', createUserHandler)8app.PUT('/users/:id', updateUserHandler)9app.PATCH('/users/:id', patchUserHandler)10app.DELETE('/users/:id', deleteUserHandler)Route handlers receive the request context and reply object. They must return a standard Response object. Returning undefined or another value results in an HTTP 500 response.
Organize routes with groups
Use app.group() to organize related routes under a shared URL prefix. You can also provide middleware that applies to the routes registered inside the group.
1app.group('/api/users', (group) => {2 group.GET('/', getUsersHandler)3 group.GET('/:id', getUserHandler)4}, authMiddleware)The group prefix is combined with each route path, while group middleware is included in the registered routes' middleware pipeline.
Load application modules
The modules() method loads dependency injection modules and their registered providers, controllers and routers. It also makes the application instance available through the dependency injection container.
1app.modules([2 UsersModule,3 AuthModule,4])Load your modules before starting the server so their registered routes and dependencies are available to the application.
Initialize Socket.IO
Lykos provides a Socket.IO integration through app.socketIO(). It initializes the Socket.IO server using the Bun Socket.IO engine and returns the Socket.IO server instance.
1const io = app.socketIO({2 cors: {3 origin: 'https://app.example.com'4 }5})Calling socketIO() again returns the existing Socket.IO instance rather than creating another one. You can also pass the instance through the module context when loading modules.
Inspect registered routes
Use readRoutes() to retrieve the application's registered route metadata. It returns a read-only array of registered routes.
1const routes = app.readRoutes()2 3console.log(routes)Start the HTTP server
Once your application is configured, call listen() to start the HTTP server. Lykos uses Bun.serve() internally.
1const server = app.listen(4000, () => {2 console.log('Server ready at http://localhost:4000')3})The default port is 4000. The callback is optional and runs after the server has been created. The method returns the Bun server instance.
Application API reference
The following methods are available on a Lykos application instance:
GET(path, handler, ...middlewares)— register a GET route.POST(path, handler, ...middlewares)— register a POST route.PUT(path, handler, ...middlewares)— register a PUT route.PATCH(path, handler, ...middlewares)— register a PATCH route.DELETE(path, handler, ...middlewares)— register a DELETE route.HEAD(path, handler, ...middlewares)andOPTIONS(path, handler, ...middlewares)— register HEAD and OPTIONS routes.use(middleware)— register global middleware.group(prefix, callback, ...middlewares)— register routes under a shared prefix.modules(classes, context?)— load dependency injection modules.resolve(moduleClass, token)— resolve a dependency from a loaded module.socketIO(options?)— initialize or retrieve the Socket.IO integration.readRoutes()— inspect registered route metadata.listen(port?, callback?)— start the HTTP server.