ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [typescript] Start express with typescript
    Programing/javascript 2019. 12. 6. 16:43

     

    1.Make a PROJECT_DIRECTORY

    $ mkdir plantonline-cron-service && cd $_

     

    2. Make package.json

    $ npm init -y

    // package.json
    
    {
      "name": "plantonline-cron-service",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC"
    }
    

     

    3. Install modules

    $ npm install -S express

    $ npm install -D typescript 

    $ npm install -D @tpyes/node @types/express

     

    4. Make tsconfig.json

    $ npx tsc -init

    // tsconfig.json
    
    {
      "compilerOptions": {
    
        "target": "es6",                          
        "module": "commonjs",                     
        "outDir": "./dist",                        
        "strict": true,                           
        "moduleResolution": "node",            
        "esModuleInterop": true,                  
        "forceConsistentCasingInFileNames": true  
      },
      "include": [
        "src/**/*"
      ]
    }

     

    5.Write source code

    $ mkdir src

    $ nano src/index.ts

    //src/index.ts
    
    import express from "express";
    const app = express();
    
    app.get(`/`, (req: any, res: any) => {
      res.send(`Hello World!`);
    });
    
    app.listen(3000, () => {
      console.log(`Example app listening on port 3000!`);
    });
    

    6.Install tslint & typescript as global

    $ yarn global add tslint typescript

    go tslint

     

    7. Make tslint.json

    $ tslint --init

    // tslint.json
    
    {
        "defaultSeverity": "error",
        "extends": [
            "tslint:recommended"
        ],
        "jsRules": {},
        "rules": {
            "no-console": false
        },
        "rulesDirectory": []
    }

     

    $ nano package.json

    // package.json
    
    {
      "name": "plantonline-cron-service",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "tslint": "tslint -c tslint.json 'src/**/*.ts'",
        "start": "tsc && node dist"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "dependencies": {
        "express": "^4.17.1"
      },
      "devDependencies": {
        "@types/express": "^4.17.2",
        "@types/node": "^12.12.14"
      }
    }

     

     

    8. Get check typescript code & Start

    $ npm run tslint

     

    $ npm start

     

     

    ※ 

    // dist/index.js
    
    "use strict";
    var __importDefault = (this && this.__importDefault) || function (mod) {
        return (mod && mod.__esModule) ? mod : { "default": mod };
    };
    Object.defineProperty(exports, "__esModule", { value: true });
    const express_1 = __importDefault(require("express"));
    const app = express_1.default();
    app.get(`/`, (req, res) => {
        res.send(`Hello World!`);
    });
    app.listen(3000, () => {
        console.log(`Example app listening on port 3000!`);
    });
    

     

    'Programing > javascript' 카테고리의 다른 글

    curl to fetch  (0) 2020.03.14
    How to NaN to 0 in javascript?  (0) 2019.12.11
    [javascript] histogram modules  (0) 2019.11.26
    [typescript] histogram algorithm  (0) 2019.11.26
    [node] Excel file export  (0) 2019.11.21

    댓글

Designed by Tistory.