ReastAPIs - Next.js
by Tomas Trescakยท APIs

Root Folder
Not Attempted
NameProgress
Introduction
Not Read
๐Ÿฐ Player
Not Read
Setup
Not Attempted
NameProgress
Software Installation
Not Read
Project Setup
Not Read
Running and Testing
Not Read
React and ReactDOM
Not Read
๐Ÿ’ก Assignment 1: Welcome Message
Not Attempted
Submissions
Not Read
React
Not Attempted
NameProgress
JSX and Components
Not Read
Props
Not Read
๐Ÿ‘พ Exercise: Props
Not Attempted
CSS Styles
Not Read
useState and Hooks
Not Read
๐Ÿ‘พ Exercise: useState
Not Attempted
Conditional Rendering
Not Read
Lists
Not Read
๐Ÿ‘พ Exercise: Lists
Not Attempted
Forms and Events
Not Read
๐Ÿ‘พ Exercise: Forms
Not Attempted
๐Ÿ’ก Assignment 2: Front End
Not Attempted
Advanced React
Not Attempted
NameProgress
Pure Components - memo
Not Read
Lifecycle - useEffect
Not Read
Expensive? - useMemo
Not Read
DOM Interactions - useRef
Not Read
forwardRef
Not Read
useImperativeHandle
Not Read
๐Ÿ‘พ useImperativeHandle
Not Attempted
Context
Not Read
useCallback
Not Read
useId
Not Read
useReducer
Not Read
Infrastructure
Not Attempted
NameProgress
Database
Not Read
NextAuth and Github Authentication
Not Read
Prisma and ORM
Not Read
Project Setup
Not Read
Project Authentication
Not Read
APIs
APIs - Slides
Not Attempted
Rest APIs
Rest APIs - Express.js
ReastAPIs - Next.js
Securing APIs
Securing APIs - NextAuth
tRPC
Not Attempted
NameProgress
tRPC
Not Read
tRPC - Routers
Not Read
tRPC - Server Rendering
Not Read
tRPC - Client Rendering
Not Read
Persisting Data
Not Read
Assignment 3: APIs
Not Read
0 / 60 XP

First, make sure that your express application is not running by pressing โ€œCTRL+Cโ€ in the terminal where you launched the express application.

Now, please open your project and add a new file route.js to the following directory src/app/api/tasks/route.ts. As we mentioned previously, Next is using โ€œfile-based routingโ€ with the root of your application stored in the src/app folder. Adding a new route.ts folder the the giben location, you have essentially added a new route at http://localhost:3000/api/tasks.

Listing Tasks

We will now add a new route handler into the new route.ts file. Please us the following code:

import { NextResponse, type NextRequest } from "next/server";

// In-memory data store for tasks
const tasks = [
  { id: 1, description: "Complete the project report", completed: false },
  { id: 2, description: "Clean the house", completed: true }
];

function listTasks(req: NextRequest, res: NextResponse) {
  return NextResponse.json(tasks);
}

// export individual handlers as REST commands
export { listTasks as GET };

The main difference with express.js is that we export individual handlers as the REST commands they represent. Please run your next.js project with:

pnpm dev

Then, in a separate terminal run the following command:

curl http://localhost:3000/api/tasks

Modifying Tasks

Before you just copy, paste the solution below. Try to implement the rest of the handles yourself! But if you are eager how the solution looks:

import { NextResponse, type NextRequest } from "next/server";

// In-memory data store for tasks
const tasks = [
  { id: 1, description: "Complete the project report", completed: false },
  { id: 2, description: "Clean the house", completed: true }
];

function listTasks(req: NextRequest, res: NextResponse) {
  return NextResponse.json(tasks);
}

async function addTask(req: NextRequest, res: NextResponse) {
  const { description, completed = false } = await req.json();
  const newTask = {
    id: tasks.length + 1,
    description,
    completed
  };
  tasks.push(newTask);
  return NextResponse.json(newTask, { status: 201 });
}

async function updateTask(req: NextRequest) {
  const { id, description, completed } = await req.json();
  const taskIndex = tasks.findIndex((task) => task.id === id);

  if (taskIndex !== -1) {
    tasks[taskIndex] = { id, description, completed };
    return NextResponse.json(tasks[taskIndex]);
  }

  return NextResponse.json({ message: "Task not found" }, { status: 404 });
}

async function deleteTask(req: NextRequest) {
  const { id } = await req.json();
  const taskIndex = tasks.findIndex((task) => task.id === id);

  if (taskIndex !== -1) {
    tasks.splice(taskIndex, 1);
    return NextResponse.json({ message: "Task deleted" });
  }

  return NextResponse.json({ message: "Task not found" }, { status: 404 });
}

// export individual handlers as REST commands
export {
  deleteTask as DELETE,
  listTasks as GET,
  addTask as POST,
  updateTask as PUT
};

๐Ÿ‘พ Exercise

Apply the same test procedures as in the previous exercise to test your API functionality. Make sure to use /api/tasks route not just /tasks.

Maggie

Discuss with Maggie
Use the power of generative AI to interact with course content

Discussion

0 comments
Loading editor ...
Remember to be polite and report any undesirable behaviour

Category

Empty

Labels

Discussion has no labels

1 participant

user avatar

Priority

Notifications

You're not receiving notifications from this thread.
Course Outline