Skip to content

Configuration ​

The configuration is done via decorators or/and during the bootstrap of your application.

You can configure your server with the @Configuration decorator or by using the new configuration() function, depending on your needs or preference.

Here are all examples to configure your application:

ts
import {Configuration} from "@tsed/di";
import {MyController} from "./controllers/manual/MyController";

@Configuration({
  mount: {
    "/rest": [
      `./controllers/current/**/*.ts`, // deprecated
      MyController // support manual import
    ],
    "/rest/v0": [
      // versioning
      `./controllers/v0/users/*.js`, // deprecated
      `!./controllers/v0/groups/old/*.ts` // Exclusion
    ]
  }
})
export class Server {}
typescript
import {configuration, injectable} from "@tsed/di";

configuration({
  mount: {
    "/rest": [
      `./controllers/current/**/*.ts`, // deprecated
      MyController // support manual import
    ],
    "/rest/v0": [
      // versioning
      `./controllers/v0/users/*.js`, // deprecated
      `!./controllers/v0/groups/old/*.ts` // Exclusion
    ]
  }
});
ts
import {$log} from "@tsed/logger";
import {PlatformExpress} from "@tsed/platform-express";
import {Server} from "./Server.js";

async function bootstrap() {
  try {
    $log.debug("Start server...");
    const platform = await PlatformExpress.bootstrap({
      rootModule: Server
      // extra settings
    });

    await platform.listen();
    $log.debug("Server initialized");
  } catch (er) {
    $log.error(er);
  }
}

bootstrap();

Note

Configuration set during the .bootstrap() method will be the default configuration for the application. Configuration set in the @Configuration decorator will be merged with the default configuration.

Note 2

Ts.ED supports ts-node. Ts extension will be replaced by a Js extension if ts-node isn't the runtime.

Configuration Sources ​

Since Ts.ED v8.9.0, you can use @tsed/config to load configuration from different sources (files, environment variables, redis, etc.).

See the Configuration Sources page for more information.

Legacy

For Ts.ED under v8.9.0, read the following Load configuration from file page.

Server options ​

See all available options in the ServerOptions page.

Specific Platform options ​

See specific platform options for:

Get configuration ​

The configuration can be reused throughout your application in different ways.

From service ​

typescript
import {Configuration, Injectable} from "@tsed/di";

@Injectable()
export class MyService {
  constructor(@Configuration() configuration: Configuration) {}
}
typescript
import {configuration, injectable} from "@tsed/di";

export class MyService {
  constructor() {
    const settings = configuration();
  }
}

injectable(MyService);

From decorators ​

Decorators Constant and Value can be used in all classes including:

Constant and Value accepts an expression as parameters to inspect the configuration object and return the value.

ts
import {Env} from "@tsed/core";
import {Constant, Value, Injectable} from "@tsed/di";

@Injectable()
export class MyService {
  @Constant("env")
  env: Env;

  @Value("swagger.path")
  swaggerPath: string;

  $onInit() {
    console.log(this.env);
  }
}
ts
import {constant, refValue, injectable} from "@tsed/di";
import {Env} from "@tsed/core";

export class MyClass {
  env = constant<Env>("env");
  swaggerPath = refValue<string>("swagger.path");

  $onInit() {
    console.log(this.env);
  }
}

injectable(MyClass);

WARNING

Constant returns an Object.freeze() value.

Released under the MIT License.