๋ฐ˜์‘ํ˜•

Path Parameter : /books/888 (888 ๋ถ€๋ถ„์ด ํŒŒ๋ผ๋ฏธํ„ฐ)

Query Parameter : /search?sort=price (sort=price ๋ถ€๋ถ„์ด ์ฟผ๋ฆฌ)

 

์ฟผ๋ฆฌ ์ŠคํŠธ๋ง(Query String)์€ /search?sort=price ์ฃผ์†Œ์—์„œ ? ๋ฌผ์Œํ‘œ ์ดํ›„์— ๋‚˜์˜ค๋Š” ์ „์ฒด ๋ฌธ์ž์—ด์„ ์˜๋ฏธํ•˜๊ณ , ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ(Query Parameter)๋Š” ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง ๋‚ด์˜ key=value ์Œ์„ ์˜๋ฏธํ•œ๋‹ค.

 

req.params


// ์š”์ฒญ URL : /books/888
router.get('/books/:id', (req, res, next) => {
  console.log(req.params); // { id: '888' }
});

 

req.query


// URL-encoded ๋ฐ์ดํ„ฐ ํŒŒ์‹ฑ์€ ์ฃผ๋กœ ํผ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ์ถœํ•  ๋•Œ ์‚ฌ์šฉ
// { extended: false } -> NodeJS ๊ธฐ๋ณธ ๋‚ด์žฅ๋œ querystring ๋ชจ๋“ˆ ์‚ฌ์šฉ
// { extended: true } -> qs ๋ชจ๋“ˆ ์‚ฌ์šฉ. ์ค‘์ฒฉ ๊ฐ์ฒด ํŒŒ์‹ฑ ๊ฐ€๋Šฅ
app.use(express.urlencoded({ extended: false }));

// ์š”์ฒญ URL : /search?sort=price
// axios ์‚ฌ์šฉ์‹œ : axios.get('/search', { params: { sort: 'price' } })
router.get('/search', (req, res, next) => {
  console.log(req.query); // { sort: 'price' }
});
๋ฐ˜์‘ํ˜•