1. Tuntau
  2. Learn
  3. Beacon

Beacon

Write servers with modules, controllers and injected services. Flare compiles them into plain functions at build time, and existing NestJS apps run unchanged.

PreviewPart of Flare
On this page
  1. Start a project
  2. A first controller
  3. How it works
  4. What's supported
  5. Running a NestJS app
  6. Switches

Preview. Beacon ships inside Flare, which has no public release yet.

Start a project

flare init my-api -t api
cd my-api
flare src/main.ts          # run in development
flare build                # one bundled file for production

Like NestJS, Beacon needs "experimentalDecorators": true in tsconfig.json.

A first controller

import { Beacon, Body, Controller, Get, Injectable, Module, Param, Post, MinLength } from '@flare/beacon';

class CreateCat {
  @MinLength(2) name: string;
  age: number;
}

@Injectable()
class CatsService {
  private cats: CreateCat[] = [];
  add(cat: CreateCat) { this.cats.push(cat); return cat; }
  find(id: number) { return this.cats[id]; }
}

@Controller('cats')
class CatsController {
  constructor(private cats: CatsService) {}

  @Get(':id')
  find(@Param('id') id: number) { return this.cats.find(id); }

  @Post()
  create(@Body() body: CreateCat) { return this.cats.add(body); }
}

@Module({ controllers: [CatsController], providers: [CatsService] })
class AppModule {}

const app = await Beacon.create(AppModule);
await app.listen(3000);

id: number is checked and converted from the URL without a pipe, and CreateCat is validated from its types and rules. Unknown properties are dropped.

How it works

  1. Compile. Before your code runs, Flare reads the TypeScript types: constructor parameter types become dependency-injection tokens, and parameter and property types become validation rules. No reflect-metadata at run time.
  2. Generate. At start-up Beacon writes one straight-line handler per route, one validator per DTO class and a router of nested switch statements.
  3. Fall back. Routes the generator doesn't cover (middleware, @Res(), server-sent events, redirects, request-scoped controllers, wildcards) run through general handlers.

The framework adds about 0.04 µs per request over a hand-written handler.

What's supported

What's supported
AreaSupported
Modules@Module, @Global, dynamic modules (forRoot/register), re-exports, forwardRef
Providersclasses, useValue, useFactory, useExisting, @Inject, @Optional, ModuleRef; default, transient and request scopes
Routes@Get/@Post/@Put/@Patch/@Delete/@All, @HttpCode, @Header, @Redirect, @Sse, global prefix, URI versioning
Parameters@Param, @Query, @Body, @Headers, @Req, @Res, @Ip, custom decorators
Enhancersguards, interceptors, pipes (ValidationPipe, ParseIntPipe, …), exception filters, middleware
ConfigConfigModule.forRoot, ConfigService, .env files
OpenAPIgenerated from types; @ApiTags, @ApiProperty, Swagger UI via useOpenAPI(app, { path })
Real-timeserver-sent events; WebSocket gateways (@WebSocketGateway, @SubscribeMessage)
HTTPHTTP/1.1; HTTPS and HTTP/2 via httpsOptions; HTTP/3 with http3: true
TestingTest.createTestingModule, app.getHttpServer() for supertest

Running a NestJS app

Imports from @nestjs/common, core, testing, config, swagger, mapped-types and platform-express/-fastify resolve to Beacon, so an existing NestJS app runs with flare src/main.ts. Not mapped yet: socket.io gateways, multipart uploads, microservices, GraphQL and CQRS.

Switches

Switches
VariableEffect
BEACON_DEBUG=1log the generated routes
BEACON_COMPILE=0no code generation (general handlers only)
FLARE_NEST=nodedon't map @nestjs/* to Beacon