[Node][Express] all & use 메소드

2024. 8. 16. 20:07·Backend/Node.js

 Express는 각 http 메소드를 통해 특정 엔드포인트에 접근할 때 미들웨어 함수를 호출할 수 있습니다.

여기에 더해 express는 다른 메소드 함수도 제공하고 있습니다.

all( )

all 함수는 모든 종류의 http 메소드에 해당합니다.

따라서 POST, GET 등의 메소드 종류에 관계 없이 미들웨어를 호출합니다.

const app = express()

// all은 모든 메소드에서 호출됩니다.
app.all('/endpoint', (req, res, next) => {
    console.log('all method called')
    next()
})

// GET 메소드에서만 호출됩니다.
app.get('/endpoint', (req, res, next) => {
    console.log('GET method called')
    res.json({ value: 'get'})
    next()
})

app.listen(3000, () => {
    console.log('Server is listening on port 3000');
});

위 코드를 기반으로 엔드포인트 /endpoint로 GET 메소드를 호출한다면, app.all( )의 미들웨어와 app.get( )의 미들웨어 총 두 개의 미들웨어 함수가 호출됩니다.

// app.all()과 app.get() 총 두 개의 미들웨어를 호출합니다.
$ GET https://localhost:3000/endpoint

all method called
GET method called

use( )

use 함수 또한 http 메소드와 관계 없이 지정한 미들웨어를 호출하게 합니다.

all과 다른 점은 엔드포인트의 경로도 특정할 수 있다는 것입니다.

all의 경우 모든 메소드에 호출되지만, 지정한 엔드포인트로 향하지 않는 메소드에는 호출되지 않습니다.

use는 지정한 엔트포인트를 지나는(= 하위에 있는) 엔드포인트들에도 호출됩니다.

const app = express()

// /endpoint를 거치는 모든 엔드포인트로 향하는 모든 메소드에서 호출됩니다.
app.use('/endpoint', (req, res, next) => {
    console.log('use method called')
    next()
})

// /endpoint/field1로 향하는 모든 메소드에서 호출됩니다.
app.all('/endpoint/field1', (req, res, next) => {
    console.log('all method called')
    next()
})

// /endpoint/field2로 향하는 GET 메소드에서만 호출됩니다.
app.get('/endpoint/field2', (req, res, next) => {
    console.log('GET method called')
    res.json({ value: 'get'})
    next()
})

app.listen(3000, () => {
  console.log('Server is listening on port 3000');
});

위 코드를 기반으로 GET https://localhost:3000/endpoint/field2 를 요청하면 all의 미들웨어는 호출되지 않습니다.

하지만 use의 /endpoint를 지나기 때문에 use의 미들웨어는 호출됩니다.

// app.use()와 app.get() 총 두 개의 미들웨어를 호출합니다.
$ GET https://localhost:3000/endpoint/field2

use method called
GET method called

use( )의 엔드포인트는 생략 가능합니다. 이때, use는 모든 엔드포인트에 대해 호출됩니다.

app.use('/endpoint', (req, res, next) => {
    console.log('use method called')
    next()
})
728x90

'Backend > Node.js' 카테고리의 다른 글

[Node][Express] Middleware | 01. cookie-parser  (0) 2024.08.19
[Node][Express] 내장 미들웨어 json() / urlencoded() / static()  (0) 2024.08.18
[Node][Express] Express로 미들웨어 다루기  (0) 2024.08.12
[Node][Express] Express에 대해서  (0) 2024.08.06
[Node] npm init & install  (0) 2024.05.19
'Backend/Node.js' 카테고리의 다른 글
  • [Node][Express] Middleware | 01. cookie-parser
  • [Node][Express] 내장 미들웨어 json() / urlencoded() / static()
  • [Node][Express] Express로 미들웨어 다루기
  • [Node][Express] Express에 대해서
Rayi
Rayi
  • Rayi
    아카이브
    Rayi
  • 전체
    오늘
    어제
    • 분류 전체보기 (262)
      • CS (40)
        • ML (3)
        • CV (2)
        • PS (34)
      • Reveiw (17)
        • Paper (17)
        • Github (0)
      • Pytorch (5)
      • Language (58)
        • Python (7)
        • JavaScript (32)
        • TypeScript (16)
        • C++ (3)
      • IDE (12)
      • Git (13)
      • Frontend (71)
        • React (8)
        • SolidJS (20)
        • CSS (12)
      • UI (3)
      • Backend (15)
        • DB (17)
        • Node.js (11)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    backend
    UI
    frontend
    CS
    python
    postgresql
    C++
    API
    DB
    js
    Three
    PyTorch
    CSS
    mongo
    ML
    html
    vscode
    react
    SOLID
    PRISMA
    GAN
    figma
    CV
    Git
    deploy
    nodejs
    ps
    review
    ts
    Express
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
Rayi
[Node][Express] all & use 메소드
상단으로

티스토리툴바