๋ด๋ณด๋ด๋ฒ(๋ด๊ฐ ๋ณด๋ ค๊ณ ๋ด๊ฐ ๋ฒ์ญํ...) Next.js docs
2021๋ 4์ 15์ผ ๊ธฐ์ค Next.js ๊ณต์ ๋ฌธ์๋ฅผ ๋ฒ์ญํ๋ค.
โป ์์ด ์ ๊ณต์๋ ํด์ธ ์ ํํ๋ ์๋๊ธฐ์ ๋ฒ์ญ์๋ ์์ญ, ์ค์ญ, ๊ตฌ๊ธ ๋ฒ์ญ์ด ๋ฌด์ํ ๋ง์ ์ ์์ผ๋ฉฐ, ํผ์ ๊ณต์๋ฌธ์๋ฅผ ์ฐธ์กฐํด๊ฐ๋ฉฐ ๋ฒ์ญํ๋ค ๋ณด๋ ์คํ๋ ๋ง์ ์ ์๋ค. ์ ํํ ๋ด์ฉ์ ๊ณต์๋ฌธ์๋ฅผ ์ง์ ์ดํด๋ณด๊ฑฐ๋ ๋ค๋ฅธ ์ ๋ณด๋ค์ ๋ ์ฐพ์๋ณด๋ ๊ฒ์ ์ถ์ฒํ๋ค.
(ํ์ง๋ง ๋๊ธ ํผ๋๋ฐฑ๋ ํ์ํฉ๋๋ค๐ )
Next.js ๊ณต์๋ฌธ์ ํ์ธํ๊ธฐ>>
API routes provide built in middlewares which parse the incoming request (req). Those middlewares are:
API ๋ผ์ฐํธ๋ ๋ฐ์ ์์ฒญ(req)์ ๊ตฌ๋ฌธ ๋ถ์ํ๋ ๋ด์ฅ ๋ฏธ๋ค์จ์ด๋ฅผ ์ ๊ณตํ๋ค. ๋ฏธ๋ค์จ์ด์๋:
- req.cookies - An object containing the cookies sent by the request. Defaults to {}
- req.query - An object containing the query string. Defaults to {}
- req.body - An object containing the body parsed by content-type, or null if no body was sent
- req.cookes - ์์ฒญ์ ์ํด ์ ๋ฌ๋ฐ์ ์ฟ ํค๋ฅผ ํฌํจํ๋ ๊ฐ์ฒด. ๊ธฐ๋ณธ๊ฐ์ {}
- req.query - ์ฟผ๋ฆฌ ์คํธ๋ง์ ํฌํจํ๋ ๊ฐ์ฒด. ๊ธฐ๋ณธ๊ฐ์ {}
- req.body - content-type์ผ๋ก ๊ตฌ๋ฌธ ๋ถ์๋ body ํน์ ์ ์ก๋ body๊ฐ ์๋ ๊ฒฝ์ฐ๋ null์ ํฌํจํ๋ ๊ฐ์ฒด
Custom config
Every API route can export a config object to change the default configs, which are the following:
๋ชจ๋ API ๋ผ์ฐํธ๋ ๊ธฐ๋ณธ config๋ฅผ ๋ณ๊ฒฝํ๊ธฐ ์ํด์ config ๊ฐ์ฒด๋ฅผ ๋ด๋ณด๋ผ ์ ์๋๋ฐ ๋ค์๊ณผ ๊ฐ๋ค:
export const config = {
api: {
bodyParser: {
sizeLimit: '1mb',
},
},
}
The api object includes all configs available for API routes.
api ๊ฐ์ฒด๋ API ๋ผ์ฐํธ์ ์ฌ์ฉ๊ฐ๋ฅํ ๋ชจ๋ ๊ตฌ์ฑ์ ํฌํจํ๋ค.
'bodyParser' Enables body parsing, you can disable it if you want to consume it as a Stream:
bodyParse๋ body ๊ตฌ๋ฌธ ๋ถ์์ ํ์ฑํํ๊ณ Stream์ผ๋ก ์ฌ์ฉํ๊ณ ์ถ์ผ๋ฉด ๋นํ์ฑํํ๋ฉด ๋๋ค:
export const config = {
api: {
bodyParser: false,
},
}
bodyParser.sizeLimit is the maximum size allowed for the parsed body, in any format supported by bytes, like so:
bodyParser.sizeLimit์ ๊ตฌ๋ฌธ ๋ถ์๋ body์ ํ์ฉ๋๋ ์ต๋ ํฌ๊ธฐ์ ๋ํ ๊ฒ์ผ๋ก bytes์์ ์ง์ํ๋ ์ด๋ค ํ์๋ ๊ฐ๋ฅํ๋ค:
export const config = {
api: {
bodyParser: {
sizeLimit: '500kb',
},
},
}
externalResolver is an explicit flag that tells the server that this route is being handled by an external resolver like express or connect. Enabling this option disables warnings for unresolved requests.
externalResolver๋ ์ด ๋ผ์ฐํธ๋ express, connect ๊ฐ์ ์ธ๋ถ resolver์ ์ํด ์ฒ๋ฆฌ๋๋ค๋ ๊ฒ์ ์๋ฒ์ ์๋ ค์ฃผ๊ธฐ ์ํ ๋ช
์์ ์ธ ํ๋๊ทธ์ด๋ค. ์ด ์ต์
์ ํ์ฑํํ๋ฉด ํด๊ฒฐ๋์ง ์์ ์์ฒญ์ ๋ํ ๊ฒฝ๊ณ ๋ ๋นํ์ฑํ๋๋ค.
export const config = {
api: {
externalResolver: true,
},
}
Connect/Express middleware support
You can also use Connect compatible middleware.
Connect ํธํ ๋ฏธ๋ค์จ์ด๋ ์ฌ์ฉํ ์ ์๋ค.
For example, configuring CORS for your API endpoint can be done leveraging the cors package.
์๋ฅผ ๋ค๋ฉด, API ์๋ํฌ์ธํธ์ ๋ํ CORS ๊ตฌ์ฑ์ cors ํจํค์ง๋ฅผ ํ์ฉํจ์ผ๋ก ์ํ๋ ์ ์๋ค.
First, install cors:
๋จผ์ cors๋ฅผ ์ค์นํ๋ค:
npm i cors
# or
yarn add cors
Now, let's add cors to the API route:
์ด์ API ๋ผ์ฐํธ์ cors๋ฅผ ์ถ๊ฐํ์:
import Cors from 'cors'
// Initializing the cors middleware
const cors = Cors({
methods: ['GET', 'HEAD'],
})
// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(req, res, fn) {
return new Promise((resolve, reject) => {
fn(req, res, (result) => {
if (result instanceof Error) {
return reject(result)
}
return resolve(result)
})
})
}
async function handler(req, res) {
// Run the middleware
await runMiddleware(req, res, cors)
// Rest of the API logic
res.json({ message: 'Hello Everyone!' })
}
export default handler
Go to the API Routes with CORS example to see the finished app
API Routes with CORS ์์ ์์ ์์ฑ๋ ์ฑ์ ํ์ธํ ์ ์๋ค.
Extending the req/res objects with TypeScript
For better type-safety, it is not recommended to extend the req and res objects. Instead, use pure functions to work with them:
๋ ๋์ ํ์
์์ ์ฑ์ ์ํด์ req์ res ๊ฐ์ฒด๋ฅผ ํ์ฅํ๋ ๊ฒ์ ๊ถ์ฅํ์ง ์๋๋ค. ๋์ ์์ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ํ์ฅํ ์ ์๋ค.
// utils/cookies.ts
import { serialize, CookieSerializeOptions } from 'cookie'
import { NextApiResponse } from 'next'
/**
* This sets `cookie` using the `res` object
*/
export const setCookie = (
res: NextApiResponse,
name: string,
value: unknown,
options: CookieSerializeOptions = {}
) => {
const stringValue =
typeof value === 'object' ? 'j:' + JSON.stringify(value) : String(value)
if ('maxAge' in options) {
options.expires = new Date(Date.now() + options.maxAge)
options.maxAge /= 1000
}
res.setHeader('Set-Cookie', serialize(name, String(stringValue), options))
}
// pages/api/cookies.ts
import { NextApiRequest, NextApiResponse } from 'next'
import { setCookie } from '../../utils/cookies'
const handler = (req: NextApiRequest, res: NextApiResponse) => {
// Calling our pure function using the `res` object, it will add the `set-cookie` header
setCookie(res, 'Next.js', 'api-middleware!')
// Return the `set-cookie` header so we can display it in the browser and show that it works!
res.end(res.getHeader('Set-Cookie'))
}
export default handler
If you can't avoid these objects from being extended, you have to create your own type to include the extra properties:
๊ฐ์ฒด๋ฅผ ํ์ฅํ ์ ๋ฐ์ ์๋ค๋ฉด, ์ถ๊ฐ์ ์ธ ์์ฑ๋ค์ ํฌํจํ๊ธฐ ์ํด์ ์์ ๋ง์ ํ์
์ ์์ฑํด์ผ ํ๋ค.
// pages/api/foo.ts
import { NextApiRequest, NextApiResponse } from 'next'
import { withFoo } from 'external-lib-foo'
type NextApiRequestWithFoo = NextApiRequest & {
foo: (bar: string) => void
}
const handler = (req: NextApiRequestWithFoo, res: NextApiResponse) => {
req.foo('bar') // we can now use `req.foo` without type errors
res.end('ok')
}
export default withFoo(handler)
Keep in mind this is not safe since the code will still compile even if you remove withFoo() from the export.
withFoo()๋ฅผ ๋ด๋ณด๋ด๊ธฐ์์ ์ ๊ฑฐํด๋ ์ฌ์ ํ ์ปดํ์ผ๋๊ธฐ ๋๋ฌธ์ ์์ ํ๊ธฐ ์๋ค๋ ๊ฒ์ ๊ธฐ์ตํ๋ผ.
'Next.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ด๋ณด๋ด๋ฒ] Next.js docs - Deployment (0) | 2021.07.11 |
---|---|
[๋ด๋ณด๋ด๋ฒ] Next.js docs - Response Helpers (0) | 2021.04.17 |
[๋ด๋ณด๋ด๋ฒ] Next.js docs - Dynamic API Routes (0) | 2021.04.15 |
[๋ด๋ณด๋ด๋ฒ] Next.js docs - API Routes (0) | 2021.04.11 |
[๋ด๋ณด๋ด๋ฒ] Next.js docs - Supported Browsers and Features (0) | 2021.04.05 |