๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Next.js

[๋‚ด๋ณด๋‚ด๋ฒˆ] Next.js docs - API Routes

๋‚ด๋ณด๋‚ด๋ฒˆ(๋‚ด๊ฐ€ ๋ณด๋ ค๊ณ  ๋‚ด๊ฐ€ ๋ฒˆ์—ญํ•œ...) Next.js docs

2021๋…„ 4์›” 11์ผ ๊ธฐ์ค€ Next.js ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๋ฒˆ์—ญํ–ˆ๋‹ค.

โ€ป ์˜์–ด ์ „๊ณต์ž๋„ ํ•ด์™ธ ์œ ํ•™ํŒŒ๋„ ์•„๋‹ˆ๊ธฐ์— ๋ฒˆ์—ญ์—๋Š” ์˜์—ญ, ์˜ค์—ญ, ๊ตฌ๊ธ€ ๋ฒˆ์—ญ์ด ๋ฌด์ˆ˜ํžˆ ๋งŽ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ˜ผ์ž ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•ด๊ฐ€๋ฉฐ ๋ฒˆ์—ญํ•˜๋‹ค ๋ณด๋‹ˆ ์˜คํƒ€๋„ ๋งŽ์„ ์ˆ˜ ์žˆ๋‹ค. ์ •ํ™•ํ•œ ๋‚ด์šฉ์€ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ง์ ‘ ์‚ดํŽด๋ณด๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์ •๋ณด๋“ค์„ ๋” ์ฐพ์•„๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

(ํ•˜์ง€๋งŒ ๋Œ“๊ธ€ ํ”ผ๋“œ๋ฐฑ๋„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜ƒ )

Next.js ๊ณต์‹๋ฌธ์„œ ํ™•์ธํ•˜๊ธฐ>>

 

API Routes: Introduction | Next.js

Next.js supports API Routes, which allow you to build your API without leaving your Next.js app. Learn how it works here.

nextjs.org


API routes provide a solution to build your API with Next.js.
API ๊ฒฝ๋กœ๋Š” Next.js๋กœ API๋ฅผ ๋นŒ๋“œํ•˜๋Š” ์†”๋ฃจ์…˜์„ ์ œ๊ณตํ•œ๋‹ค. 

Any file inside the folder pages/api is mapped to /api/* and will be treated as an API endpoint instead of a page. They are server-side only bundles and won't increase your client-side bundle size.
pages/api ํด๋” ๋‚ด๋ถ€์˜ ์–ด๋–ค ํŒŒ์ผ์ด๋“  /api/*๋กœ ๋งคํ•‘๋˜๋ฉฐ ํŽ˜์ด์ง€ ๋Œ€์‹  API ์—”๋“œ ํฌ์ธํŠธ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค. ์ด๊ฒƒ๋“ค์€ ์„œ๋ฒ„ ์ธก ์ „์šฉ ๋ฒˆ๋“ค์ด๋ฉฐ ํด๋ผ์ด์–ธํŠธ ์ธก ๋ฒˆ๋“ค ํฌ๊ธฐ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค.  

For example, the following API route pages/api/user.js returns a json response with a status code of 200:
์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์˜ pages/api/user.js API ๊ฒฝ๋กœ๋Š” ์ƒํƒœ ์ฝ”๋“œ 200๊ณผ ํ•จ๊ป˜ json ์‘๋‹ต์„ ๋ฆฌํ„ดํ•œ๋‹ค: 

export default function handler(req, res) {
  res.status(200).json({ name: 'John Doe' })
}

For an API route to work, you need to export a function as default (a.k.a request handler), which then receives the following parameters:
API ๊ฒฝ๋กœ๊ฐ€ ๋™์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•จ์ˆ˜๋ฅผ default๋กœ (์ฆ‰, ์š”์ฒญ ํ•ธ๋“ค๋Ÿฌ) ๋‚ด๋ณด๋‚ด์•ผํ•˜๋Š”๋ฐ, ๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์„ ๋ฐ›๋Š”๋‹ค. 

  • req: An instance of http.IncomingMessage, plus some pre-built middlewares you can see here
  • res: An instance of http.ServerResponse, plus some helper functions you can see here
  • req: http.IncomingMessage์˜ ์ธ์Šคํ„ด์Šค์™€ ์ด๊ณณ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋Š” ์ผ๋ถ€ ๋ฏธ๋ฆฌ ๋นŒ๋“œ๋œ ๋ฏธ๋“ค์›จ์–ด
  • res: http.ServerResponse์˜ ์ธ์Šคํ„ด์Šค์™€ ์ด๊ณณ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋Š” ์ผ๋ถ€ ํ—ฌํผ ํ•จ์ˆ˜

To handle different HTTP methods in an API route, you can use req.method in your request handler, like so:
API ๊ฒฝ๋กœ์—์„œ ๋‹ค๋ฅธ HTTP ๋ฉ”์†Œ๋“œ๋“ค์„ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์š”์ฒญ ํ•ธ๋“ค๋Ÿฌ ๋‚ด์—์„œ req.method๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค:

export default function handler(req, res) {
  if (req.method === 'POST') {
    // Process a POST request
  } else {
    // Handle any other HTTP method
  }
}

To fetch API endpoints, take a look into any of the examples at the start of this section.
API ์—”๋“œ ํฌ์ธ๋“œ๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด, ์ด ์„น์…˜์˜ ์‹œ์ž‘ํ•˜๋Š” ๋ถ€๋ถ„์— ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์‹œ๋ฅผ ์ฐธ์กฐํ•˜๋ผ. (๋”๋ณด๊ธฐ ์ฐธ์กฐ)

Use Cases ์‚ฌ์šฉ ์‚ฌ๋ก€

For new projects, you can build your entire API with API Routes. If you have an existing API, you do not need to forward calls to the API through an API Route. Some other use cases for API Routes are:
์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ๋ฅผ ์œ„ํ•ด์„œ API ๊ฒฝ๋กœ๋กœ ์ „์ฒด API๋ฅผ ๋นŒ๋“œํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฏธ API๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ, API ๊ฒฝ๋กœ๋ฅผ ํ†ตํ•ด์„œ ํ•ด๋‹น API๋กœ ํ˜ธ์ถœ์„ ์ „๋‹ฌํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. API ๊ฒฝ๋กœ์˜ ๋‹ค๋ฅธ ์‚ฌ์šฉ ์‚ฌ๋ก€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค:

  • Masking the URL of an external service (e.g. /api/secret instead of https://company.com/secret-url)
  • Using Environment Variables on the server to securely access external services.
  • ์™ธ๋ถ€ ์„œ๋น„์Šค์˜ URL ๋งˆ์Šคํ‚น (์˜ˆ. https://company.com/secret-url ๋Œ€์‹  /api/secret)
  • ์„œ๋ฒ„์—์„œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉ์—ฌ ์•ˆ์ „ํ•˜๊ฒŒ ์™ธ๋ถ€ ์„œ๋น„์Šค์— ์ ‘๊ทผ

Caveats ์ฃผ์˜ ์‚ฌํ•ญ

  • API Routes do not specify CORS headers, meaning they are same-origin only by default. You can customize such behavior by wrapping the request handler with the cors middleware.
  • API Routes can't be used with next export
  • API ๊ฒฝ๋กœ๋Š” CORS ํ—ค๋”๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋™์ผ ์ถœ์ฒ˜์ž„์„ ์˜๋ฏธํ•œ๋‹ค. ์š”์ฒญ ํ—ค๋”๋ฅผ cors ๋ฏธ๋“ค์›จ์–ด๋กœ ๊ฐ์‹ธ๋ฏ€๋กœ ์ด๋Ÿฌํ•œ ๋™์ž‘์— ๋Œ€ํ•ด ์‚ฌ์šฉ์ž ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. 
  • API ๊ฒฝ๋กœ๋Š” next export์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.