Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Server

Hierarchy

  • ServerLoader
    • Server

Implements

  • IServerLifecycle

Index

Constructors

constructor

  • Returns Server

Properties

expressApp

expressApp: Express.Application

Return Express Application instance.

returns

httpServer

httpServer: Server

Return Http.Server instance.

returns

httpsServer

httpsServer: Server

Return Https.Server instance.

returns

injector

injector: InjectorService

Return the injectorService initialized by the server.

returns

injectorService

injectorService: InjectorService

Return the InjectorService initialized by the server.

returns

settings

settings: ServerSettingsService

Return the settings configured by the decorator @ServerSettings.

serversettings({

rootDir: Path.resolve(__dirname), port: 8000, httpsPort: 8080, mount: { "/rest": "${rootDir}/controllers/*\/.js" } }) export class Server extends ServerLoader { $onInit(){ console.log(this.settings); // {rootDir, port, httpsPort,...} } } `

@returns {ServerSettingsService}

version

version: string

Methods

$onInit

  • $onInit(): Promise<any>

$onMountingMiddlewares

  • $onMountingMiddlewares(): Promise<any>
  • This method let you configure the middleware required by your application to works.

    Returns Promise<any>

$onReady

  • $onReady(): void

$onServerInitError

  • $onServerInitError(err: any): void
  • Parameters

    • err: any

    Returns void

addComponents

  • addComponents(classes: any | any[], options?: Partial<IComponentScanned>): ServerLoader
  • Add classes to the components list

    Parameters

    • classes: any | any[]
    • Optional options: Partial<IComponentScanned>

    Returns ServerLoader

addControllers

  • addControllers(endpoint: string, controllers: any[]): ServerLoader
  • Add classes decorated by @Controller() to components container.

    Example

    @Controller('/ctrl')
    class MyController{
    }
    
    new ServerLoader().addControllers('/rest', [MyController])
    

    ?> If the MyController class isn't decorated, the class will be ignored.

    Parameters

    • endpoint: string
    • controllers: any[]

    Returns ServerLoader

createHttpServer

  • createHttpServer(port: string | number): ServerLoader
  • Create a new HTTP server with the provided port.

    Parameters

    • port: string | number

    Returns ServerLoader

createHttpsServer

  • createHttpsServer(options: IHTTPSServerOptions): ServerLoader
  • Create a new HTTPs server.

    options :

    • port <number>: Port number,
    • 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.

    See more info on httpsOptions.

    Parameters

    • options: IHTTPSServerOptions

      Options to create new HTTPS server.

    Returns ServerLoader

engine

  • engine(ext: string, fn: Function): ServerLoader
  • Proxy to express engine

    Parameters

    • ext: string
    • fn: Function

    Returns ServerLoader

Protected getSettingsService

  • getSettingsService(): ServerSettingsService
  • Returns ServerSettingsService

Protected loadMiddlewares

  • loadMiddlewares(): Promise<any>
  • Initialize configuration of the express app.

    Returns Promise<any>

Protected loadSettingsAndInjector

  • loadSettingsAndInjector(): Promise<any>
  • Returns Promise<any>

mount

  • mount(endpoint: string, list: any | string | any[]): ServerLoader
  • Mount all controllers files that match with globPattern (Glob Pattern) under the endpoint. See Versioning Rest API for more informations.

    Parameters

    • endpoint: string
    • list: any | string | any[]

    Returns ServerLoader

scan

  • scan(patterns: string | string[], endpoint?: string): ServerLoader
  • Scan and imports all files matching the pattern. See the document on the Glob pattern for more information.

    Example

    import {ServerLoader} from "@tsed/common";
    import Path = require("path");
    
    export class Server extends ServerLoader {
    
       constructor() {
           super();
    
           let appPath = Path.resolve(__dirname);
    
           this.scan(appPath + "/controllers/**\/**.js")
      }
    }
    

    Theses pattern scan all files in the directories controllers, services recursively.

    !> On windows on can have an issue with the Glob pattern and the /. To solve it, build your path pattern with the module Path.

    const controllerPattern = Path.join(rootDir, 'controllers','**','*.js');
    

    Parameters

    • patterns: string | string[]
    • Optional endpoint: string

    Returns ServerLoader

set

  • set(setting: string, val: any): ServerLoader
  • Proxy to express set

    Parameters

    • setting: string
    • val: any

    Returns ServerLoader

Protected setSettings

  • setSettings(settings: IServerSettings): void
  • Parameters

    • settings: IServerSettings

    Returns void

start

  • start(): Promise<any>
  • Start the express server.

    Returns Promise<any>

Protected startServer

  • startServer(http: Server | Server, settings: object): Promise<object>
  • Create a new server from settings parameters.

    Parameters

    • http: Server | Server
    • settings: object
      • address: string | number
      • https: boolean
      • port: number

    Returns Promise<object>

use

  • use(...args: any[]): ServerLoader
  • This method let you to add a express middleware or a Ts.ED middleware like GlobalAcceptMimes.

    @ServerSettings({
       rootDir,
       acceptMimes: ['application/json'] // optional
    })
    export class Server extends ServerLoader {
        $onMountingMiddlewares(): void|Promise<any> {
            const methodOverride = require('method-override');
    
            this.use(GlobalAcceptMimesMiddleware)
                .use(methodOverride());
    
            // similar to
            this.expressApp.use(methodOverride());
    
            // but not similar to
            this.expressApp.use(GlobalAcceptMimesMiddleware); // in this case, this middleware will not be added correctly to express.
    
            return null;
        }
    }
    

    Parameters

    • Rest ...args: any[]

    Returns ServerLoader

Static cleanGlobPatterns

  • cleanGlobPatterns(files: string | string[], excludes: string[]): string[]
  • Parameters

    • files: string | string[]
    • excludes: string[]

    Returns string[]

Generated using TypeDoc