Name | Progress |
---|---|
Introduction | Not Read |
๐ฐ Player | Not Read |
Name | Progress |
---|---|
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 |
Name | Progress |
---|---|
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 |
Name | Progress |
---|---|
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 |
Name | Progress |
---|---|
Database | Not Read |
NextAuth and Github Authentication | Not Read |
Prisma and ORM | Not Read |
Project Setup | Not Read |
Project Authentication | Not Read |
Name | Progress |
---|---|
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 |
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.
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
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
};
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.