
사용 방법
//route.ts
import { NextRequest, NextResponse } from "next/server";
export async function GET(request: NextRequest) {
console.log(request);
return NextResponse.json({
ok: true,
});
}
export async function POST(request: NextRequest) {
const data = await request.json();
console.log("log in yuser", data);
return NextResponse.json(data);
}
"use client";
export default function Home(){
const onClick = async () => {
const response = await fetch("/api/users", {
method: "POST",
body: JSON.stringify({
username: "nico",
password: "1234",
}),
});
};
return <form onClick={onClick}>....</form>
}
일반적으로 react를 이용해서 login data를 보내면 useEffect, state, fetch를 이용하지만 nextjs14에서 많은 부분이 생략 되었다.
✍️ 특징 및 장점
const onSubmit = async (data: FormData) => {
"use server";
console.log(data.get("email"), data.get("password"));
/// xxx.gmail.com, 1111
};
<form action={onSubmit}>
<input name="email"/> // ex) xxx@gmail.com
<input name="password"/> // ex) 1111
</form>