Tech Blog of Pinomaker

1. Node Project 초기화

작업할 폴더를 생성하고 아래의 명령어로 Node Project로 초기화를 해준다.

npm init -y

 

2. Module 설치

프로젝트를 위한 모듈을 설치한다.

 

이제 배포 할 때 사용하지 않는 모듈은 --save-dev 옵션을 사용하여 설치하는 데, 주로 typescript의 모듈을 그렇게 설치한다.

 

Typescript로 Node를 진행하기 위해서는 기존의 모듈을 설치하고, @types의 모듈도 배포용으로 설치를 해줘야한다.

 

따라서 아래와 같이 모듈을 설치한다.

npm install typescript express ts-node

npm install --save-dev @types/express nodemon

 

3. 폴더 구조 생성, index.ts 생성

 

위의 폴더 구조를 참고하여, src/index.ts를 생성하고 아래와 같이 작성한다.

// src/index.ts

import express, { Request, Response } from 'express'

const app = express()

app.get('/', async (req: Request, res: Response) => {
  res.send('Hello Typescript')
})

const port: number = 3000
app.listen(port, () => console.log(`SERVER ON! PORT : ${port}`))

 

4. 프로젝트 실행 명령 스크립트 추가

package.json 파일에서 scripts 부분에 프로젝트를 실행할 명령어를 아래와 같이 추가하여 프로젝트를 실행할 경우 Typescipt를 컴파일 후 실행한다.

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    // 추가한 스크립트 명령어
    "dev": "nodemon --watch \"src/**/*.ts\" --exec \"ts-node\" src/index.ts"
  },

 

 

5. Project 실행

npm run dev

 

 

위의 명령어로 프로젝트를 실행하면 아래와 같이 실행이 잘 되는 것을 볼 수 있다.

profile

Tech Blog of Pinomaker

@pinomaker

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!