[Express] req.query vs req.params
๋ฐ์ํ
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' }
});
๋ฐ์ํ
'๐ช Programming' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React/JS] ๋๋๊ทธํ ๋ฌธ์์ด ๋ถ๋ฆฌ(๋ฉํ)ํ๊ธฐ / Selection API (0) | 2024.05.21 |
---|---|
[React] ๋ฆฌ์กํธ ๋์์ฑ ๋ ๋๋ง(Concurrent) ํบ์๋ณด๊ธฐ (1) | 2024.05.20 |
[JS] ํจ์ํ / ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ๋ฎ์ ๊ผด (0) | 2024.05.19 |
[TS] TypeScript ํ์ ์คํฌ๋ฆฝํธ Infer ํค์๋ ํ์ฉํ๊ธฐ (0) | 2024.05.19 |
[JS] JavaScript ์๋ฐ์คํฌ๋ฆฝํธ Map ๋ ์ ์ฌ์ฉํ๊ธฐ (0) | 2024.05.19 |
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote
๋ค๋ฅธ ๊ธ
-
[React/JS] ๋๋๊ทธํ ๋ฌธ์์ด ๋ถ๋ฆฌ(๋ฉํ)ํ๊ธฐ / Selection API
[React/JS] ๋๋๊ทธํ ๋ฌธ์์ด ๋ถ๋ฆฌ(๋ฉํ)ํ๊ธฐ / Selection API
2024.05.21 -
[React] ๋ฆฌ์กํธ ๋์์ฑ ๋ ๋๋ง(Concurrent) ํบ์๋ณด๊ธฐ
[React] ๋ฆฌ์กํธ ๋์์ฑ ๋ ๋๋ง(Concurrent) ํบ์๋ณด๊ธฐ
2024.05.20 -
[JS] ํจ์ํ / ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ๋ฎ์ ๊ผด
[JS] ํจ์ํ / ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ๋ฎ์ ๊ผด
2024.05.19 -
[TS] TypeScript ํ์ ์คํฌ๋ฆฝํธ Infer ํค์๋ ํ์ฉํ๊ธฐ
[TS] TypeScript ํ์ ์คํฌ๋ฆฝํธ Infer ํค์๋ ํ์ฉํ๊ธฐ
2024.05.19