๋ด๋ณด๋ด๋ฒ(๋ด๊ฐ ๋ณด๋ ค๊ณ ๋ด๊ฐ ๋ฒ์ญํ...) Next.js docs
2021๋ 4์ 13์ผ ๊ธฐ์ค Next.js ๊ณต์ ๋ฌธ์๋ฅผ ๋ฒ์ญํ๋ค.
โป ์์ด ์ ๊ณต์๋ ํด์ธ ์ ํํ๋ ์๋๊ธฐ์ ๋ฒ์ญ์๋ ์์ญ, ์ค์ญ, ๊ตฌ๊ธ ๋ฒ์ญ์ด ๋ฌด์ํ ๋ง์ ์ ์์ผ๋ฉฐ, ํผ์ ๊ณต์๋ฌธ์๋ฅผ ์ฐธ์กฐํด๊ฐ๋ฉฐ ๋ฒ์ญํ๋ค ๋ณด๋ ์คํ๋ ๋ง์ ์ ์๋ค. ์ ํํ ๋ด์ฉ์ ๊ณต์๋ฌธ์๋ฅผ ์ง์ ์ดํด๋ณด๊ฑฐ๋ ๋ค๋ฅธ ์ ๋ณด๋ค์ ๋ ์ฐพ์๋ณด๋ ๊ฒ์ ์ถ์ฒํ๋ค.
(ํ์ง๋ง ๋๊ธ ํผ๋๋ฐฑ๋ ํ์ํฉ๋๋ค๐ )
Next.js ๊ณต์๋ฌธ์ ํ์ธํ๊ธฐ>>
API routes support dynamic routes, and follow the same file naming rules used for pages.
API ๋ผ์ฐํธ๋ ๋์ ๊ฒฝ๋ก๋ฅผ ์ง์ํ๊ณ ํ์ด์ง์์๋ ์ฌ์ฉ๋์๋ ๊ฒ๊ณผ ๊ฐ์ ๋ช
๋ช
๊ท์น์ ๋ฐ๋ฅธ๋ค.
For example, the API route pages/api/post/[pid].js has the following code:
์๋ฅผ ๋ค์ด, API ๋ผ์ฐํธ pages/api/post/[pid].js๋ ์๋์ ๊ฐ์ ์ฝ๋๋ฅผ ํฌํจํ๋ค:
export default function handler(req, res) {
const { pid } = req.query
res.end(`Post: ${pid}`)
}
Now, a request to /api/post/abc will respond with the text: Post: abc.
์ด์ /api/post/abc์ ๋ํ ์์ฒญ์ Post: abc๋ผ๋ ํ
์คํธ๋ก ์๋ต๋ฐ๋๋ค.
Index routes and Dynamic API routes ์ธ๋ฑ์ค ๋ผ์ฐํธ์ ๋์ API ๋ผ์ฐํธ
A very common RESTful pattern is to set up routes like this:
๋ผ์ฐํธ๋ฅผ ์ค์ ํ๊ธฐ ์ํ ๋งค์ฐ ๋ณดํธ์ ์ธ RESTful ํจํด์ ์ด์ ๊ฐ๋ค:
- GET api/posts - gets a list of posts, probably paginated
- GET api/posts/12345 - gets post id 12345
- GET api/posts - ํฌ์คํธ ๋ชฉ๋ก์ ๋ฐ๋๋ค. ์๋ง๋ ํ์ด์ง ๋งค๊น์ด ๋์ด์๋
- GET api/posts/12345 - id๊ฐ 12345์ธ ํฌ์คํธ๋ฅผ ๋ฐ๋๋ค
We can model this in two ways:
์ด๊ฒ์ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ๋ชจ๋ธํ ์ ์๋ค:
- Option 1:
- /api/posts.js
- /api/posts/[postId].js
- Option 2:
- /api/posts/index.js
- /api/posts/[postId].js
Both are equivalent. A third option of only using /api/posts/[postId].js is not valid because Dynamic Routes (including Catch-all routes - see below) do not have an undefined state and GET api/posts will not match /api/posts/[postId].js under any circumstances.
๋ ๊ฐ์ง ๋ฐฉ๋ฒ ๋ชจ๋ ๊ฐ๋ค. ๋จ /api/posts/[postId].js๋ง ์ฌ์ฉํ๋ ์ธ ๋ฒ์งธ ์ต์
์ ๋์ ๊ฒฝ๋ก(๋ชจ๋ ๋ผ์ฐํธ ํฌ์ฐฉ(Catch-all routes)์ ํฌํจํด์ - ํ๋จ ๋ด์ฉ ์ฐธ์กฐ)๊ฐ undefined ์ํ๋ฅผ ๊ฐ์ง ์ ์๊ณ GET api/posts๊ฐ /api/posts/[postId].js ์๋ ์ด๋ค ๊ฒฝ์ฐ์๋ ๋งค์น๋์ง ์๊ธฐ ๋๋ฌธ์ ์ด ์ต์
์ ์ ํจํ์ง ์๋ค.
Catch all API routes ๋ชจ๋ API ๋ผ์ฐํธ ํฌ์ฐฉ
API Routes can be extended to catch all paths by adding three dots (...) inside the brackets. For example:
API ๋ผ์ฐํธ๋ ์ ์ธ ๊ฐ(...)๋ฅผ ๊ดํธ ์์ ์ถ๊ฐํจ์ผ๋ก ๋ชจ๋ ๋ผ์ฐํธ๋ฅผ ํฌ์ฐฉํ๊ธฐ ์ํด ํ์ฅ๋ ์ ์๋ค. ์๋ฅผ ๋ค๋ฉด:
- pages/api/post/[...slug].js matches /api/post/a, but also /api/post/a/b, /api/post/a/b/c and so on.
- pages/api/post/[...slug].js ๋ /api/post/a์ ๋งค์น๋์ง๋ง /api/post/a/b, /api/post/a/b/c ๋ฑ์ ๊ฒฝ๋ก์๋ ๋งค์น๋๋ค.
๋ ธํธ: slug๋ง๊ณ [...param] ๊ฐ์ด ๋ค๋ฅธ ์ด๋ฆ๋ ์ฌ์ฉํ ์ ์๋ค.
Matched parameters will be sent as a query parameter (slug in the example) to the page, and it will always be an array, so, the path /api/post/a will have the following query object:
๋งค์น๋ ํ๋ผ๋ฏธํฐ๋ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ(์์์ slug)๋ก ํ์ด์ง์ ๋ณด๋ด์ง๋ค. ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ ํญ์ ๋ฐฐ์ด์ด๋ฉฐ, /api/post/a ๊ฒฝ๋ก๋ ์๋์ ๊ฐ์ ์ฟผ๋ฆฌ ๊ฐ์ฒด๋ฅผ ๊ฐ๋๋ค:
{ "slug": ["a"] }
And in the case of /api/post/a/b, and any other matching path, new parameters will be added to the array, like so:
๊ทธ๋ฆฌ๊ณ /api/post/a/b์ ๊ฒฝ์ฐ์ ๊ฒฝ๋ก๊ฐ ๋งค์น๋๋ ๋ค๋ฅธ ๊ฒฝ์ฐ์๋ ์๋ก์ด ํ๋ผ๋ฏธํฐ๋ค์ด ๋ฐฐ์ด์ ์๋์ ๊ฐ์ด ์ถ๊ฐ๋๋ค:
{ "slug": ["a", "b"] }
An API route for pages/api/post/[...slug].js could look like this:
pages/api/post/[...slug].js๋ฅผ ์ํ API ๋ผ์ฐํธ๋ ์๋์ ๊ฐ๋ค:
export default function handler(req, res) {
const { slug } = req.query
res.end(`Post: ${slug.join(', ')}`)
}
Now, a request to /api/post/a/b/c will respond with the text: Post: a, b, c.
์ด์ /api/post/a/b/c์ ๋ํ ์์ฒญ์ `Post: a, b, c` ํ
์คํธ๋ก ์๋ต๋ฐ์ ๊ฒ์ด๋ค.
Optional catch all API routes ์ ํ์ API ๋ผ์ฐํธ ํฌ์ฐฉ
Catch all routes can be made optional by including the parameter in double brackets ([[...slug]]).
์ด์ค ๋๊ดํธ ์์ ํ๋ผ๋ฏธํฐ๋ฅผ ํฌํจํ์ฌ ๋ชจ๋ ๋ผ์ฐํธ๋ฅผ ์ ํ์ ์ผ๋ก ํฌ์ฐฉ(catch)ํ ์ ์๋ค. ([[...slug]])
For example, pages/api/post/[[...slug]].js will match /api/post, /api/post/a, /api/post/a/b, and so on.
์๋ฅผ ๋ค๋ฉด, pages/api/post/[[...slug]].js๋ /api/post, /api/post, /api/post/a, /api/post/a/b ๋ฑ๊ณผ๋ ๋งค์น๋๋ค.
The main difference between catch all and optional catch all routes is that with optional, the route without the parameter is also matched (/api/post in the example above).
๋ชจ๋ ๋ผ์ฐํธ๋ฅผ ํฌ์ฐฉํ๋ ๊ฒ(Catch-all routes)๊ณผ ๋ชจ๋ ๋ผ์ฐํธ๋ฅผ ์ ํ์ ์ผ๋ก ํฌ์ฐฉํ๋ ๊ฒ์ ์ฃผ์ ์ฐจ์ด์ ์ ์ ํ์ ์ธ ํฌ์ฐฉ์ ๊ฒฝ์ฐ ํ๋ผ๋ฏธํฐ๊ฐ ์์ด๋ ๋งค์น๊ฐ ๋๋ค๋ ๊ฒ์ด๋ค. (์์ ์์ ์์ /api/post)
The query objects are as follows:
์ฟผ๋ฆฌ ๊ฐ์ฒด๋ ๋ค์๊ณผ ๊ฐ๋ค:
{ } // GET `/api/post` (empty object)
{ "slug": ["a"] } // `GET /api/post/a` (single-element array)
{ "slug": ["a", "b"] } // `GET /api/post/a/b` (multi-element array)
Caveats ์ฃผ์์ฌํญ
- Predefined API routes take precedence over dynamic API routes, and dynamic API routes over catch all API routes. Take a look at the following examples:
๋ฏธ๋ฆฌ ์ ์๋ API ๋ผ์ฐํธ๋ ๋์ API ๋ผ์ฐํธ๋ณด๋ค ์ฐ์ ์์๊ฐ ๋๊ณ ๋์ API ๋ผ์ฐํธ๋ ๋ชจ๋ API ๋ผ์ฐํธ ํฌ์ฐฉ๋ณด๋ค ์ฐ์ ์์๊ฐ ๋๋ค. ์๋์ ์์๋ค์ ๋ณด์:
- pages/api/post/create.js - Will match /api/post/create
pages/api/post/create.js๋ /api/post/create์ ๋งค์น๋๋ค. - pages/api/post/[pid].js - Will match /api/post/1, /api/post/abc, etc. But not /api/post/create
pages/api/post/[pid].js๋ /api/post/1, /api/post/abc ๋ฑ๊ณผ ๋งค์น ๋์ง๋ง /api/post/create์๋ ๋งค์น๋์ง ์๋๋ค. - pages/api/post/[...slug].js - Will match /api/post/1/2, /api/post/a/b/c, etc. But not /api/post/create, /api/post/abc
pages/api/post/[...slug].js๋ /api/post/1/2, /api/post/a/b/c ๋ฑ๊ณผ ๋งค์น ๋์ง๋ง /api/post/create, /api/post/abc์๋ ๋งค์น๋์ง ์๋๋ค.
- pages/api/post/create.js - Will match /api/post/create
'Next.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ด๋ณด๋ด๋ฒ] Next.js docs - Response Helpers (0) | 2021.04.17 |
---|---|
[๋ด๋ณด๋ด๋ฒ] Next.js docs - API Middlewares (0) | 2021.04.17 |
[๋ด๋ณด๋ด๋ฒ] Next.js docs - API Routes (0) | 2021.04.11 |
[๋ด๋ณด๋ด๋ฒ] Next.js docs - Supported Browsers and Features (0) | 2021.04.05 |
[๋ด๋ณด๋ด๋ฒ] Next.js docs - Environment Variables (0) | 2021.04.04 |