-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-routes.ts
More file actions
73 lines (60 loc) · 2.44 KB
/
api-routes.ts
File metadata and controls
73 lines (60 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// api-routes.ts
// -------------------------------------------
// Next.js 15 - API Routes with App Router
//
// In Next.js 15 App Router, API routes are implemented
// as route handler files inside the `app/api/` folder.
//
// This file explains:
// - What are API Routes
// - How to create API route handlers
// - HTTP method handlers (GET, POST, etc.)
// - Using in-memory or external data
// - Example API route for todos
// -------------------------------------------
// 1️⃣ What are API Routes?
// API Routes let you create backend endpoints inside your Next.js app.
// They can handle requests (GET, POST, PUT, DELETE) and respond with JSON or other data.
// Useful for building REST APIs, form submissions, authentication, etc.
// 2️⃣ How to create API Routes?
// - Create a folder inside `app/api/` for your endpoint.
// - Add a `route.ts` file inside that folder.
// - Export functions named after HTTP methods: GET, POST, PUT, DELETE, etc.
// - Each handler receives a Request object and returns a Response.
// 3️⃣ Example of API Route Handler (todos)
// ---------------CODE-START--------------- //
// File: app/api/todos/route.ts
// import { NextResponse } from "next/server";
// // In-memory todos array (for example only)
// let todos = [
// { id: 1, title: "Learn Next.js 15 API routes", completed: false },
// { id: 2, title: "Build a Todo app", completed: false },
// ];
// // GET handler - Return all todos
// export async function GET() {
// return NextResponse.json(todos);
// }
// // POST handler - Add a new todo
// export async function POST(request: Request) {
// const data = await request.json();
// const newTodo = {
// id: todos.length + 1,
// title: data.title,
// completed: false,
// };
// todos.push(newTodo);
// return NextResponse.json(newTodo, { status: 201 });
// }
// ---------------CODE-END--------------- //
// -------------------------------------------
// Notes:
// - Use NextResponse to send responses.
// - You can parse JSON body from Request with `request.json()`.
// - You can also handle other HTTP methods similarly.
// - This approach works with app router's file-based routing.
// -------------------------------------------
// 4️⃣ Summary:
// - API routes live in app/api/your-endpoint/route.ts
// - Export functions named by HTTP verbs (GET, POST, etc.)
// - Use Request and NextResponse to handle requests/responses
// - Great for backend logic within Next.js without separate server