Support Lithia.js by becoming a sponsor at

Menu

Routing

Routing files

The Lithia.js routing system enables you to define routes based on the file system. Each file inside the routes folder represents a route handler, and each route handler should always export default an async function following this signature:

routes/hello.ts

import { LithiaRequest, LithiaResponse } from 'lithia/types';

export default async function handler(req: LithiaRequest, res: LithiaResponse) {
  res.send('Hello, world!');
}

When running build or dev command, Lithia.js will automatically detect your handlers and set up all configurations for you.


The best part about this routing system is that Lithia.js keeps the running server and the defined route-handlers in different layers, this feature allows you to perform changes without restarting the server.


Using HTTP methods

With Lithia.js, you can define your HTTP methods by adding the method name to the file name. For example, to create a GET route, that points to /hello, you can create a file named hello.get.ts. Note that if you don't specify a method, your route will be available for all methods. Sometimes it could be useful.


When defining a method, takes care of the following rules:


  • The method name should always be in lowercase.
  • The method name should be separated by a dot (.) from the route name.
  • The method name should be the first part of the file name.