Function: staticUI()
staticUI(
directory,options?):RequestHandler
Create an express handler serving a built web UI (the dist directory of a Vite application): its files, and its index for the paths of its client-side routes. Given as the fallback of expressHTTPEntrypoint, it never receives the paths of the actions. It needs the optional peer dependency express.
The files with a content hash in their name are cached for a long time (assetsMaxAge), the other files and the index are revalidated on each request. A request matching no file that is not a page request (an asset, a non-GET request, a client not accepting HTML) is passed on to the next handler.
Parameters
directory
string
The directory of the built UI, resolved from the current working directory.
options?
How the index is served and the assets cached.
Returns
RequestHandler
The express request handler.
Example
import {Application} from 'lakutata'
import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
import {GET} from 'lakutata/decorator/ctrl'
import {expressHTTPEntrypoint, staticUI} from 'lakutata/adapter/express'
class DeviceController extends Controller {
//GET /api/devices answers the action; GET /settings answers the index of the UI
@GET('/api/devices')
public async list(): Promise<string[]> {
return ['sensor-1']
}
}
Application.run({
id: 'console.app',
name: 'Console',
components: {
entrypoint: buildEntrypoints({
controllers: [DeviceController],
http: expressHTTPEntrypoint({
port: 8080,
fallback: staticUI('./ui/dist', {assetsMaxAge: 86400})
})
})
},
bootstrap: ['entrypoint']
})