API Routes
API Routes๋ API๋ฅผ ํธ์ถํ ์ ์๋ Endpoint๋ฅผ ์์ฑํด์ฃผ๋๋ฐ, ์ฝ๊ฒ ๋งํด๋ณด์๋ฉด Next.js์ ํ์ผ ๊ธฐ๋ฐ์ผ๋ก API๋ฅผ ๋ง๋ค ์ ์๊ฒํด์ค๋ค. ๊ทธ๋ฌ๋๊น ๋ค๋ฅธ ๋ฐฑ์๋ ํ๋ ์์ํฌ ํ์์์ด, ๋ฐ๋ก api๋ฅผ ๋ง๋ค์ด์ ์ฌ์ฉํ์ง ์์๋ DB ์ฐ๊ฒฐ๊น์ง ํ ์ ์๊ณ , ๋ฐ์ดํฐ ์ ์ก์ด ๊ฐ๋ฅํ๋ค๋ ๋ง์ด๋ค. Routing๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ํ์ผ๊ธฐ๋ฐ์ผ๋ก ์ด๋ฃจ์ด์ง๋๋ฐ Nextjs14์ ๊ฒฝ์ฐ๋ pages๊ฐ ์๋ app ํด๋์ ํ์ผ์ ์์ฑํ๊ณ (14๋ฒ์ ผ ์ด์ ์ pages/api), ํจ์๋ฅผ ์์ฑํ๋ฉด ๋๋ค.
API Routes๋ Serverless function์ผ๋ก ์ด๋ฃจ์ด์ ธ์๋ค. Serverless Function์ ์ปจ์ ์ ์์ฒญ(์ด๋ฒคํธ)์ด ๋ค์ด์ฌ ๋๋ง๋ค ์ง์ ๋ ํจ์๋ฅผ ์คํ์์ผ ์ํ๋ API๋ฅผ ํด๋ผ์ด์ธํธ์๊ฒ ์ ๋ฌํ๋ ๊ฒ์ด๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ web socket๊ณผ ๊ฐ์ด ์ง์์ ์ธ ์ปค๋ฅ์ ์ ๊ฐ์ง ์ ์๋ค. ํ์ง๋ง ๊ฐ๋จํ ๋ฐ์ดํฐ ์ ์ก๊ณผ ์บ์ฑ ๊ธฐ๋ฅ์ด ์๊ธฐ๋๋ฌธ์ ๋ณต์กํ ์๋น์ค๊ฐ ์๋๋ผ๋ฉด ์์ฃผ ๋น ๋ฅด๊ณ ์ถ๊ฒ ์๋น์ค๋ฅผ ๊ตฌ์ถํ ์ ์๊ฒ ํด์ค๋ค.
์ฌ์ฉ ๋ฐฉ๋ฒ
api๋ก ์ฃผ๊ณ ๋ฐ์ ๋ฐ์ดํฐ ์คํค๋ง ์์ฑํด์ค๋๋ค. ๋ฌผ๋ก db์๋ ๋๊ฐ์ด ์ด๋ฐ ํํ๋ก ๋ค์ด๊ฐ๊ฑฐ๊ณ ๊ฐ์ ธ์ฌ๊ฑฐ๋ค. db๋ mongoDB๋ฅผ ์ฌ์ฉํ๊ณ , ์ฐ๊ฒฐ์ mongoose๋ฅผ ์ด์ฉํ๋ค.
models.js
import mongoose from "mongoose";
const recordsSchema = new mongoose.Schema(
{
userId: {
type: Number,
required: true,
unique:true,
},
username:{
type: String,
required: true,
unique: true,
minlength: 3,
maxlength: 10
},
time: {
type: Number,
required: true,
},
section:{
type: String,
required: true,
minlength: 1,
maxlength: 50
},
},
{timestamps: true }
)
export const Record = mongoose.models?.Record || mongoose.model("Record", recordsSchema);
/api/routes.js
export const POST = async(req, res) => {
try {
// db ์ฐ๊ฒฐ
ConnectToDb();
const data = await req.json()
// db์ ์ ์ฅํด์ฃผ๊ธฐ, ๋ง๋ค์ด ๋ ์คํค๋ง๊ฐ์ ธ์์ ์๋ก ์์ฑํด์ ์ ์ฅํด์ฃผ๊ธฐ
const newRecord = new Record(data);
await newRecord.save();
// post๋ผ์ response๋ฅผ ์ด๋ ๊ฒ ์ค ํ์๋ ์์ง๋ง get ์ฌ์ฉํ ๋๋ ํ์
return NextResponse.json(data);
} catch (err) {
console.log(err);
throw new Error("Failed to fetch posts!");
}
}
revalidatePath ์ด๋ผ๊ณ next์์ ์ ๊ณตํ๋ ํจ์์ธ๋ฐ, /URL์ ์๋ ์บ์๋ฅผ ์ญ์ ํ๊ณ ๋ค์ ์์ฑ, ํ์ด์ง๋ฅผ ๋ค์ ๋ก๋ํด์ฃผ๋ ๊ธฐ๋ฅ์ด ์๋ต๋๋ค. ๋ณ๊ฒฝ๋ ๊ฒ๋ง ๋ฐ๊ฟ์ฃผ๋ ๊ธฐ๋ฅ์ด๋ผ์ ๋ฃ์ด์คฌ์ต๋๋ค. ๊ดํธ์์๋ ํด๋น ํ์ด์ง ๊ฒฝ๋ก๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋ฉ๋๋ค.
/ํด๋นํ์ผ.jsx
export async function postRecord(req) {
try {
const res = await fetch('http://localhost:3000/api/record', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(req),
});
if (!res.ok) {
throw new Error(`Failed to save record. Status: ${res.status}`);
}
} catch (error) {
console.error('Error:', error);
}
}
์๋ฒ๋ก ๋ณด๋ผ ํจ์ fetchํ ์ฝ๋. sever client๋ก ๊ตฌํํ ์๋ ์์ง๋ง ํด๋น ํ์ผ ๊ตฌ์ฑ์ ๊ทธ๋ ๊ฒ ์ธ ์ ์๋ ์ํฉ์ด์ฌ์ use client ๋ก ๊ตฌํ์ ํด์ผํ๋ค. api/record๋ ์์ post ํจ์๊ฐ ๋ค์ด๊ฐ์๋ ํ์ผ ๊ฒฝ๋ก์ด๋ค. method๋ฅผ 'POST'๋ก ํด์ฃผ๊ณ fetch ํจ์ ์ฐ๋ฏ์ด ์ฌ์ฉํ๋ฉด ๋๋ค.
์ด๋ ๊ฒ ๊ฐ๋จํ ๊ฑด๋ฐ, ์ฌ์ค ์ด ๋ถ๋ถ ๊ตฌํ์ด ํ๋ฃจ์ข ์ผ ๊ฑธ๋ ธ๋ค. 'POST' method๋ฅผ fetch๋ฅผ ํ๊ธดํ๋๋ฐ, payload๋ก ๋ฐ์ดํฐ๋ ์ ๋๋ก ๋ณด๋ด๋๋ฐ, ๊ณ์ sever error๊ฐ ๋๋ ๊ฒ์ด๋ค. api๋ฅผ ์๋๋ฐ request body ๊ฐ์ด ์ ๋๋ก ์ ๋ค์ด์จ๋ค๊ณ 500 ์๋ฌ๊ฐ ๊ณ์ ๋จ. request์ ๋ค์ด๊ฐ๋ ์์๋ค์ด ๋ค ํ์๋ผ๊ณ ์คํค๋ง์ ์ ์ด๋ฌ์ ์ ๋๋ก ์ ๋ค์ด์ค๋ฉด error ๋จ๋๊ฑฐ. ๋งจ๋ ํด๋ผ์ด์ธํธ์ชฝ๋งํด์ ์ด๊ฑธ ์ด๋ป๊ฒ ํด๊ฒฐํด์ผํ ์ง๋ ๋ชจ๋ฅด๊ฒ ๊ณ , ์ฌ์ง์ด payload์๋ ๋ค ๋์ด๊ฐ๊ณ ์๋ ์ํฉ์ด์ฌ์ ๋ ์ค๋ฆฌ๋ฌด์ค. ๋ด๊ฐ ํ์์์ ๋ค๋ฃจ์ง ๋ชป ํ๋ ๋ถ๋ถ์ด๋ผ์ ๋ ํจ๋.
์ฌ๊ธฐ์ ๊ธฐ console.log๋ฅผ ์ฐ์๋๋ดค๋๋ฐ POST ํจ์์ชฝ์์ request๋ฅผ ์ฐ์ด๋ณด๋ฉด undefined๋ก ์๋ฌด๊ฒ๋ ์ ์ฐํ๋ ์ํฉ.
const data = await req.json() ์ด ๋ถ๋ถ์ ์ ๋๋ก ์ ์ ์ด์ค์ ๊ทธ๋ฐ๊ฑฐ
์๋์ ๋ธ๋ก๊ทธ๋ฅผ ๋ณด๋ฉด post handling์ ์ด๋ป๊ฒ ํด์ผํ๋์ง ์ ํ์๋ค. ์ด๋ถ๋ ๋์๊ฐ์ ์ค๋ฅ๋ฅผ ๊ฒช์ผ์ ๋ฏ.
https://dev.to/iambstha/http-get-post-request-in-nextjs-stable-app-router-557m
HTTP GET & POST Request in NextJS Stable App Router
When the app router was released in NextJS 13.2 , being a frontend developer, I was stuck with...
dev.to
์ฐธ๊ณ ํ์ต์๋ฃ
- ๊ณต์๋ฌธ์
https://nextjs.org/docs/pages/building-your-application/routing/api-routes
Routing: API Routes | 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
'๊ฐ๋ฐ > Next.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
vercel๋ก Nextjs ๋ฐฐํฌํ๊ธฐ (0) | 2024.01.05 |
---|---|
Next.js14์์ useRouter ์ฌ์ฉํ๊ธฐ (1) | 2024.01.03 |
npm start, npm run dev ์ ์ฐจ์ด์ (1) | 2023.12.29 |
[Next.js (SSG) /cloudFront] ๋ฐฐํฌ ์ดํ reload์์ dynamic route๋ฅผ ๋ชป ํ ๋ ํด๊ฒฐ (0) | 2023.08.07 |
NextJs ์ TypeScript ํ๊ฒฝ์์ Jest , React-Testing-Library ์ ํ ํ๊ธฐ (์ฝ์ง์ค์ง๊ฒํจ...) (0) | 2022.10.28 |