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

Next.js

[๋‚ด๋ณด๋‚ด๋ฒˆ] Next.js docs - Response Helpers

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

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

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

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

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

 

API Routes: Response Helpers | Next.js

API Routes include a set of Express.js-like methods for the response to help you creating new API endpoints. Learn how it works here.

nextjs.org


The response (res) includes a set of Express.js-like methods to improve the developer experience and increase the speed of creating new API endpoints, take a look at the following example:
์‘๋‹ต(res)์—๋Š” ๊ฐœ๋ฐœ์ž์˜ ๊ฒฝํ—˜์„ ๊ฐœ์„ ํ•˜๊ณ  ์ƒˆ๋กœ์šด API ์—”๋“œํฌ์ธ๋“œ ์ƒ์„ฑ ์†๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•œ Express.js ๊ฐ™์€ ๋ฉ”์†Œ๋“œ ์„ธํŠธ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์ œ๋“ค์„ ์‚ดํŽด๋ณด๋ผ: 

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

The included helpers are:
ํฌํ•จ๋œ helper์—๋Š”: 

  • res.status(code) - A function to set the status code. code must be a valid HTTP status code
  • res.json(json) - Sends a JSON response. json must be a valid JSON object
  • res.send(body) - Sends the HTTP response. body can be a string, an object or a Buffer
  • res.redirect([status,] path) - Redirects to a specified path or URL. status must be a valid HTTP status code. If not specified, status defaults to "307" "Temporary redirect".
  • res.status(code) - ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜. ์ฝ”๋“œ๋Š” ๋ฐ˜๋“œ์‹œ ์œ ํšจํ•œ HTTP ์ƒํƒœ ์ฝ”๋“œ์—ฌ์•ผ ํ•œ๋‹ค.
  • res.json(json) - JSON ์‘๋‹ต์„ ์ „์†ก. json์€ ๋ฐ˜๋“œ์‹œ ์œ ํšจํ•œ JSON ๊ฐ์ฒด์—ฌ์•ผ๋งŒ ํ•œ๋‹ค. 
  • res.send(body) - HTTP ์‘๋‹ต์„ ์ „์†ก. body๋Š” ๋ฌธ์ž์—ด, ๊ฐ์ฒด, ๋ฒ„ํผ์ผ ์ˆ˜ ์žˆ๋‹ค. 
  • res.redirect([status,] path) - ์ง€์ •๋œ ๊ฒฝ๋กœ๋‚˜ URL๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ. status๋Š” ๋ฐ˜๋“œ์‹œ ์œ ํšจํ•œ HTTP ์ƒํƒœ ์ฝ”๋“œ์—ฌ์•ผ ํ•œ๋‹ค. status ๊ฐ’์„ ํŠน์ •ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’ "307" "Temporary redirect"์ด๋‹ค. 

To view an example using types, check out the TypeScript documentation.
ํƒ€์ž…์„ ์‚ฌ์šฉํ•œ ์˜ˆ์ œ๋ฅผ ๋ณด๋ ค๋ฉด TypeScript ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•ด๋ณด๋ผ.