Skip to content

이창민 학습

Ryu-min edited this page Apr 3, 2021 · 6 revisions
4월 2일 학습내용

TYPESCRIPT with express

기초 학습

  • 변수, 객체, 배열, 함수 등의 타입을 지정하는 것이 타입스크립트
in javascript
let a = 3
a = 'hello' //오류 없음
in typescript
let a: number = 3
a = 'hello' //오류 발생
  • interface 혹은 type으로 원하는 형태를 지정해둘 수 있다. 협업 시 일관성을 위해 하나를 정하는 것이 좋다.
    버전이 상승하며 기능 차이는 사라짐 (interface가 대세)

  • any 타입은 타입스크립트의 존재 이유를 희석시킨다
    필요하다면 generic을 사용하자

  • 반환값이 없을 땐 :void

function test(text: string):void {
  console.log(text)
}
  • 반드시 필요한 키가 아닐 땐 ?
interface people = {
  name: string,
  age?: number
}
  • interface의 public, private 설정으로 조회를 막을 수 있다
4월 3일 학습내용

TYPESCRIPT WITH EXPRESS

필요한 패키지들

- express
- nodemon
- typescript
- @types/express : express 라이브러리의 타입을 추가
- @types/node : node.js 타입을 추가
- ts-node : 타입스크립트 파일을 실행

tsconfig.json

{
  "compilerOptions": {
    "target": "ES5",
    "module": "CommonJS",
    "outDir": "./dist" //컴파일 js 생성 위치
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}

package.json  설정에 대해선 추가 공부 필요, es6는 안되나?

{
  "scripts": {
    "prestart": "tsc", // js로 컴파일
    "start": "nodemon --exec ts-node dist/www.js" // 컴파일된 파일을 실행한다.
  }
}

express 실행 방법
app.ts

import * as express from "express";

const app: express.Application = express();
const port: number = Number(process.env.PORT) || 3000;

app.get(
  "/",
  (req: express.Request, res: express.Response, next: express.NextFunction) => {
    res.send("hello typescript express!");
  }
);

app.listen(port, () => {
  console.log(`${port}번 열렸습니다!`);
});

export default app;

서버에서의 모든 코드는 req, res, next로 이루어지니까 이것만으로도 충분하리라 생각된다

TYPEORM

Clone this wiki locally