1. API 관리용 폴더를 만들고 소스코드를 위치시킨다

root/api/user/index.js 작성

let users = [

    {id:1, name:'Alice'},

    {id:2, name:'Bek'},

    {id:3, name:'Chris'}

]

 

app.get('/', (req, res) => res.send('Hello World!'))

app.get('/users', (req, res) => {

})

app.get('/users/:id', (req, res) => {

})

app.delete('/users/:id', (req, res) => {

})

app.post('/users', (req, res) => {

})

 

2. Router 클래스를 이용한다

1) 기능 : get,delete,post 함수를 Router클래스가 제공해준다

const express = require('express')

const router = express.Router()

router.use(function timeLog(req,res,next) {

  console.log('Time: ', Date.now())

  next()

})

router.get(...)

router.delete(...)

router.post(...)

module.exports = router //module로 만들어서 root의 index.js에서 사용할 수 있게 한다

 

2) root/index.js 에서는 생성한 module을 사용한다

const user = require('./api/user') //마지막 index라는 키워드는 생략이 가능하다 require('./api/user/index.js') 와 같음

app.use('/users', user) //users에 대한 모든 경로에 대해서는 user router를 사용하겠다

 

3. Router를 Control 단위로 세분화 한다

1) root/api/user/user.ctrl.js 파일을 생성한다

user/index.js에 작성했던 app.get()~ 등의 함수 코드를 다음과 같이 옮겨 올 수 있다

const index = (req, res) => { ...

}

const show = (req, res) => { ...

}

module.exports = {

  index,

  show

}

 

2) root/api/user/index.js 에서는 다음과 같이 생성한 control들을 사용할 수가 있다

const ctrl = require('./user.ctrl')

router.get('/', ctrl.index)

router.get('/:id', ctrl.show)

 

3) control이 사용하는 데이터도 함께 ctrl 소스 파일로 옮겨와야 한다

let users = [

    {id:1, name:'Alice'},

    {id:2, name:'Bek'},

    {id:3, name:'Chris'}

]

 

4. Test Case를 작성한 테스트 파일들도 소스 코드들과 같은 경로로 옮겨준다

  가령, root/api/user 폴더 안에 생성한 index.js 의 router 테스트 파일 index.spec.js은

  root/api/user/user.spec.js 으로 명명하여 옮기고 내부에서 root/index.js를 require하는 코드의 경로도 상대 경로로 바꿔준다

  => const app = require('../../index')

 

5. package.json 내에 선언한 scripts test 파일들에 대한 설정 내용도 다음과 같이 바꾼다

  "scripts" : {

    "test" : "mocha ./api/**/*.spec.js" <- api경로 아래의 모든 *.spec.js 파일들을 테스트하겠다

  }

번호 제목 글쓴이 날짜 조회 수
56 Node.js로 ES6 코드 실행하기 (Babel) file 황제낙엽 2023.01.07 370
55 자바스크립트 CommonJS 모듈 내보내기/불러오기 (require) file 황제낙엽 2023.01.07 331
54 Node.js에서 ES 모듈(import/export) 사용하기 file 황제낙엽 2023.01.07 600
53 CommonJS 와 ES 의 Module 시스템 황제낙엽 2023.01.07 337
52 [session] 기본 - Express-session 기반 로그인 세션 관리 황제낙엽 2022.01.03 308
51 [session] 기본 - 2 file 황제낙엽 2022.01.03 327
50 [session] 기본 - 1 황제낙엽 2022.01.03 326
49 [T아카데미] Node.js 프로그램 기초부터 응용까지 강좌 모음 황제낙엽 2021.03.15 235
48 Node.js 에서 Bulk INSERT 를 하는 방법 (multi insert) 황제낙엽 2021.02.02 1363
47 mysql, mysql2 모듈 버전 비교 황제낙엽 2021.01.31 595
46 [T아카데미/Node.js 프로그래밍] 프로그램 흐름 제어 (콜백지옥, Async, Promise) file 황제낙엽 2021.01.27 349
45 ORM 툴 Sequelize file 황제낙엽 2021.01.27 334
44 [connection과 pool-05] pool 생성이 너무 많은 경우의 database에서의 에러 - Too many connections file 황제낙엽 2021.01.27 483
43 [connection과 pool-04] connection pool 에서의 Transaction을 이용한 commit 과 rollback 황제낙엽 2021.01.27 341
42 [connection과 pool-03] mysql.createPool 과 connection.query 황제낙엽 2021.01.27 940
41 [connection과 pool-02] mysql.createPool 과 pool.query 황제낙엽 2021.01.27 460
40 [connection과 pool-01] mysql.createConnection 과 connection.query 황제낙엽 2021.01.27 388
39 [T아카데미/Node.js 프로그래밍/MySQL] createPool, getConnection 의 간단한 예제 (강의 예제) 황제낙엽 2021.01.27 379
38 여러 개의 데이터 베이스 연결을 동시에 관리하는 PoolCluster 황제낙엽 2021.01.16 2698
37 connetion pooling 과 connection leak 황제낙엽 2021.01.16 359