Skip to content

Function: expressHTTPEntrypoint() ​

expressHTTPEntrypoint(options?): EntrypointAdapter<HTTPContext<{ }>>

Create the HTTP adapter of an entrypoint served by an express application: each HTTP action (@GET, @POST, @HTTPAction…) becomes an express route. It needs the optional peer dependency express.

An action's input is the request's query and body (see the input option) with the route parameters merged over them. Its result is written by the responder (JSON by default, 204 for no result); a StatusResult sets the status and the headers, a FileResult sends a file, a buffer or a stream (304 when its ETag matches If-None-Match), and an EventsResult streams Server-Sent Events. An action that writes the response itself (through HTTPContext.response) is left alone. The errors are written by the responder with their status (400 for the validation failures, 500 by default).

When the client disconnects before the response, the action's abort signal fires and nothing is written. When the entrypoint stops, the server stops accepting connections, the event streams end, and the requests in progress are given up to one second after they settle to send their responses before the remaining connections are closed — the connections taken over by an upgrade (the WebSockets of the application) too, which would hold the stop forever.

Parameters ​

options? ​

ExpressHTTPEntrypointOptions

The adapter options; by default it listens on port 80 of all the interfaces with a new express application.

Returns ​

EntrypointAdapter<HTTPContext<{ }>>

The adapter, to give to the http option of buildEntrypoints.

Example ​

typescript
import {Application} from 'lakutata'
import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
import {GET, POST} from 'lakutata/decorator/ctrl'
import {expressHTTPEntrypoint, staticUI} from 'lakutata/adapter/express'

class UserController extends Controller {
    //GET /users/1?verbose=true: the route parameter id merged over the query
    @GET('/users/:id')
    public async get(input: {id: string, verbose?: string}): Promise<{id: string}> {
        return {id: input.id}
    }

    //POST /users with the JSON body {"name": "Ada"}
    @POST('/users')
    public async create(input: {name: string}): Promise<{name: string}> {
        return {name: input.name}
    }
}

Application.run({
    id: 'web.app',
    name: 'Web',
    components: {
        entrypoint: buildEntrypoints({
            controllers: [UserController],
            http: expressHTTPEntrypoint({
                port: 8080,
                body: {limit: '5mb'},
                //The application's own routes, before the actions
                before: (app): void => {
                    app.get('/health', (request, response): void => void response.send('ok'))
                },
                //The pages of the UI built in ./ui/dist, for the paths outside /users
                fallback: staticUI('./ui/dist')
            })
        })
    },
    bootstrap: ['entrypoint']
})

Released under the MIT License.