ServerSettings Decorator

Module
import { ServerSettings } from "@tsed/common"
Source/packages/common/src/server/decorators/serverSettings.ts

Overview

function ServerSettings(settings: IServerSettingsOptions): Function;

Description

@ServerSettings let you to configure quickly your server via decorator. This decorator take your configuration and merge it with the default server configuration.

The default configuration is as follow:

{
  "rootDir": "path/to/root/project",
  "env": "development",
  "port": 8080,
  "httpsPort": 8000,
  "uploadDir": "${rootDir}/uploads",
  "mount": {
    "/rest": "${rootDir}/controllers/**/*.js"
},
"componentsScan": [
   "${rootDir}/middlewares/**/*.js",
   "${rootDir}/services/**/*.js",
   "${rootDir}/converters/**/*.js"
 ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

You can customize your configuration as follow:

import {ServerLoader, ServerSettings} from "@tsed/common";
import Path = require("path");

@ServerSettings({
    rootDir: Path.resolve(__dirname),
    mount: {
        "/rest": "${rootDir}/controllers/current/**/*.js",
        "/rest/v1": [
          "${rootDir}/controllers/v1/users/*.js",
          "${rootDir}/controllers/v1/groups/*.js"
        ]
    }
})
export class Server extends ServerLoader {
    static Initialize = (): Promise<any> => new Server().start();
}

Server.Initialize();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Options

  • rootDir <string>: The root directory where you build run project.
  • env <Env>: The environment profile. By default the environment profile is equals to NODE_ENV.
  • port <string | number>: Port number for the HTTP.Server.
  • httpsPort <string | number>: Port number for the HTTPs.Server.
  • httpsOptions <Https.ServerOptions)>:
    • key <string> | <string[]> | <Buffer> | <Object[]>: The private key of the server in PEM format. To support multiple keys using different algorithms an array can be provided either as a plain array of key strings or an array of objects in the format {pem: key, passphrase: passphrase}. This option is required for ciphers that make use of private keys.
    • passphrase <string> A string containing the passphrase for the private key or pfx.
    • cert <string> | <string[]> | <Buffer> | <Buffer[]>: A string, Buffer, array of strings, or array of Buffers containing the certificate key of the server in PEM format. (Required)
    • ca <string> | <string[]> | <Buffer> | <Buffer[]>: A string, Buffer, array of strings, or array of Buffers of trusted certificates in PEM format. If this is omitted several well known "root" CAs (like VeriSign) will be used. These are used to authorize connections.
  • uploadDir <string&gt: The temporary directory to upload the documents. See more on Upload file with Multer.
  • mount <IServerMountDirectories>: Mount all controllers under a directories to an endpoint.
  • componentsScan <string[]>: List of directories to scan Services, Middlewares or Converters.
  • serveStatic <IServerMountDirectories>: Objet to mount all directories under to his endpoints. See more on Serve Static.
  • routers <object>: Global configuration for the Express.Router. See express documentation.
  • validationModelStrict <boolean>: Use a strict validation when a model is used by the converter. When a property is unknow, it throw a BadRequest. By default true.