# Migrate from Express.js

# Installation

You can get the latest version of Ts.ED using the following npm command:

    TIP

    The following modules also are recommended:

      WARNING

      It is really important to keep the same version for all @tsed/* packages. To prevent errors, fix the version for each Ts.ED packages:

      {
        "dependencies": {
          "@tsed/common": "5.56.0",
          "@tsed/di": "5.56.0",
          "@tsed/core": "5.56.0",
          "@tsed/exceptions": "5.56.0",
          "@tsed/plaftorm-express": "5.56.0",
          "@tsed/swagger": "5.56.0"
        }
      }
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10

      WARNING

      Ts.ED requires Node >= 10, Express >= 4, TypeScript >= 4.0.2 and the experimentalDecorators, emitDecoratorMetadata, types and lib compilation options in your tsconfig.json file.

      {
        "compilerOptions": {
          "baseUrl": ".",
          "outDir": "./dist",
          "target": "es2018",
          "lib": ["es7", "dom", "esnext.asynciterable"],
          "typeRoots": ["./node_modules/@types"],
          "module": "commonjs",
          "moduleResolution": "node",
          "experimentalDecorators": true,
          "emitDecoratorMetadata": true,
          "sourceMap": true,
          "declaration": false,
          "allowSyntheticDefaultImports": true,
          "allowJs": true
        },
        "include": ["./src/**/*.ts"],
        "exclude": ["node_modules", "./public", "dist", "test"]
      }
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19

      TIP

      You can use this example of package.json to add npm tasks and dependencies:

      {
        "name": "@tsed/basic-example",
        "version": "1.0.0",
        "description": "Here an example to configure your server and how to create your first controller.",
        "main": "src/index.js",
        "scripts": {
          "clean": "rimraf '{src,test}/**/*.{js,js.map}'",
          "build": "yarn tsc",
          "test": "yarn clean && yarn test:lint && yarn test:coverage",
          "test:unit": "cross-env NODE_ENV=test mocha",
          "test:coverage": "cross-env NODE_ENV=test nyc mocha",
          "test:lint": "tslint --project tsconfig.json",
          "test:lint:fix": "tslint --project tsconfig.json --fix",
          "travis:deploy-once": "travis-deploy-once",
          "travis:coveralls": "nyc report --reporter=text-lcov | coveralls",
          "tsc": "tsc --project tsconfig.json",
          "tsc:w": "tsc --project tsconfig.json -w",
          "start": "nodemon --watch \"src/**/*.ts\" --ignore \"node_modules/**/*\" --exec ts-node src/index.ts",
          "start:prod": "cross-env NODE_ENV=production node dist/index.js",
          "docker:build": "yarn build && docker-compose build",
          "deploy": "exit 0"
        },
        "author": "",
        "license": "MIT",
        "dependencies": {
          "@tsed/ajv": "6.11.3",
          "@tsed/common": "6.11.3",
          "@tsed/core": "6.11.3",
          "@tsed/di": "6.11.3",
          "@tsed/exceptions": "6.11.3",
          "@tsed/schema": "6.11.3",
          "@tsed/json-mapper": "6.11.3",
          "@tsed/platform-express": "6.11.3",
          "@tsed/swagger": "6.11.3",
          "@types/swagger-schema-official": "2.0.21",
          "body-parser": "1.19.0",
          "cors": "2.8.5",
          "compression": "1.7.4",
          "concurrently": "5.3.0",
          "cookie-parser": "1.4.5",
          "express": "4.17.1",
          "method-override": "^3.0.0",
          "uuid": "8.3.0",
          "cross-env": "7.0.2"
        },
        "devDependencies": {
          "@types/chai": "4.2.12",
          "@types/chai-as-promised": "7.1.3",
          "@types/cors": "2.8.6",
          "@types/express": "4.17.7",
          "@types/http-proxy": "1.17.2",
          "@types/mocha": "8.0.3",
          "@types/node": "14.11.1",
          "@types/request-promise": "4.1.45",
          "@types/sinon": "9.0.8",
          "@types/sinon-chai": "3.2.4",
          "@types/supertest": "2.0.10",
          "chai": "4.2.0",
          "chai-as-promised": "7.1.1",
          "concurrently": "5.3.0",
          "mocha": "8.1.3",
          "nodemon": "1.19.4",
          "nyc": "15.1.0",
          "rimraf": "3.0.0",
          "sinon": "9.0.3",
          "sinon-chai": "3.5.0",
          "supertest": "6.0.0",
          "ts-node": "9.0.0",
          "tslint": "6.1.3",
          "typescript": "4.2.3"
        }
      }
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72

      # Create server

      To use Ts.ED now, use the Platform API to create an application. Platform API gives an abstraction layer between your code written with Ts.ED and the Express code. It means a large part of your code isn't coupled with Express itself, and can be used with another Platform like Koa.

      Ts.ED provides a Configuration decorator to declare a new application. Just create a server.ts in your root project :

      import {Configuration, Inject} from "@tsed/di";
      import {PlatformApplication} from "@tsed/common";
      // import compress from "compression";
      // import cookieParser from "cookie-parser";
      // import methodOverride from "method-override";
      
      @Configuration({
        acceptMimes: ["application/json"]
      })
      export class Server {
        @Inject()
        app: PlatformApplication;
      
        @Configuration()
        settings: Configuration;
      
        /**
         * This method let you configure the express middleware required by your application to works.
         * @returns {Server}
         */
        public $beforeRoutesInit(): void | Promise<any> {
          // Add middlewares here only when all of your legacy routes are migrated to Ts.ED
          // this.app
          //   .use(cookieParser())
          //   .use(compress({}))
          //   .use(methodOverride())
        }
      }
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28

      To customize the server settings see Configuration page.

      Finally, create an index.ts file to bootstrap your server with the legacy express application, on the same level of the Server.ts:

        You should have this directory tree:

        .
        ├── src
        │   ├── legacy/server.js
        │   ├── controllers
        │   ├── services
        │   ├── middlewares
        │   ├── index.ts
        │   └── Server.ts
        └── package.json
        
        1
        2
        3
        4
        5
        6
        7
        8
        9

        TIP

        By default Ts.ED loads automatically Services, Controllers and Middlewares in specific directories. This behavior can be changed by editing the componentsScan configuration.

        Last Updated: 2/11/2022, 7:27:21 PM

        Other topics