การสร้างตัวจัดการเส้นทาง API
สร้างจุดปลายทาง API แบบ RESTful ที่ทนทานโดยใช้ตัวจัดการเส้นทางของ App Router สำหรับตรรกะฝั่งเซิร์ฟเวอร์
การสร้างตัวจัดการเส้นทาง API เป็นบทเรียน Next.js 15 Fullstack Web Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Next.js 15 Fullstack Web Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Next.js 15 Fullstack Web Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Welcome to Route Handlers
In Next.js 15, Route Handlers are the powerful way to build backend API endpoints directly within your App Router project.
They allow you to create RESTful APIs, handle webhooks, or perform any server-side logic, all co-located with your frontend code.
Why Use Route Handlers?
Route Handlers offer several benefits:
- Simplicity: No need for a separate backend server.
- Co-location: API logic lives next to related UI components.
- Performance: Built on Web Standard APIs, making them efficient.
- Flexibility: Support all HTTP methods (GET, POST, PUT, DELETE, etc.).
Creating Your First Handler
To create a Route Handler, define a route.ts (or .js) file inside an app directory segment. For example, app/api/hello/route.ts will handle requests to /api/hello.
Inside this file, you export functions that match HTTP methods, like GET, POST, etc.
Handling GET Requests
The GET function handles HTTP GET requests. It receives a NextRequest object and should return a NextResponse.
Let's create a simple 'hello world' API endpoint.
import { NextResponse } from 'next/server';
export async function GET() {
return NextResponse.json({ message: 'Hello from API!' });
}How to Access GET Handler
The previous code snippet, placed in app/api/hello/route.ts, creates an endpoint accessible at /api/hello.
When you navigate to http://localhost:3000/api/hello in your browser, you'll see the JSON response: {"message":"Hello from API!"}.
Handling POST Requests
For data submission, you'll use the POST function. It also receives the NextRequest object, which contains the request body.
You can parse the JSON body using await request.json().
import { NextRequest, NextResponse } from 'next/server';
export async function POST(request: NextRequest) {
const data = await request.json();
return NextResponse.json(
{ message: 'Data received', data },
{ status: 200 }
);
}Accessing Request Details
The NextRequest object provides rich access to request details:
request.url: The full request URL.request.headers: Request headers (e.g.,request.headers.get('Content-Type')).request.nextUrl.searchParams: Query parameters (e.g.,?name=Alice).
These are crucial for dynamic and secure API logic.
Dynamic Segments in Handlers
Just like pages, Route Handlers can have dynamic segments. For example, app/api/items/[id]/route.ts.
You can access these dynamic parameters from the params object passed to your handler function.
import { NextRequest, NextResponse } from 'next/server';
interface Params {
params: { id: string };
}
export async function GET(
request: NextRequest,
{ params }: Params
) {
const id = params.id; // '123' for /api/items/123
return NextResponse.json({ itemId: id, message: 'Fetched item' });
}Setting Status and Headers
When returning a NextResponse, you can customize the HTTP status code and headers.
This is important for proper API communication, like indicating success (200, 201) or errors (400, 404, 500).
import { NextResponse } from 'next/server';
export async function DELETE() {
return new NextResponse(null, { status: 204 }); // No Content
}Quick Check: Handler Basics
You want to create an API endpoint at /api/products that returns a list of products when a GET request is made.
Which code snippet correctly sets up this handler in app/api/products/route.ts?
Recap: Building API Handlers
You've learned the fundamentals of Next.js 15 Route Handlers!
- They allow backend logic co-located in the
appdirectory. - Define handlers by exporting functions (
GET,POST, etc.) in aroute.tsfile. - Use
NextRequestto access request details andNextResponseto send responses. - Dynamic segments enable flexible API routes.
This powerful feature streamlines fullstack development in Next.js.
คำถามที่พบบ่อย
บทเรียน “การสร้างตัวจัดการเส้นทาง API” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสร้างตัวจัดการเส้นทาง API” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Next.js 15 Fullstack Web Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Next.js 15 Fullstack Web Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสร้างตัวจัดการเส้นทาง API”
สร้างจุดปลายทาง API แบบ RESTful ที่ทนทานโดยใช้ตัวจัดการเส้นทางของ App Router สำหรับตรรกะฝั่งเซิร์ฟเวอร์ คุณปฏิบัติ Next.js 15 Fullstack Web Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Next.js 15 Fullstack Web Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Next.js 15 Fullstack Web Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การสร้างตัวจัดการเส้นทาง API” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Next.js 15 Fullstack Web Apps นี้ได้ไหม
ได้ บทเรียน Next.js 15 Fullstack Web Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างตัวจัดการเส้นทาง API
- การตรวจสอบคำขอและความปลอดภัย
- การผสานรวมบริการภายนอก
- การจำกัดอัตราและการจัดการข้อผิดพลาดของ API