Next.js
Server-rendered React.js framework
src/app/api/blogs/route.ts
import { NextRequest, NextResponse } from "next/server"
import clientPromise from "@/db/mongodb"
export const GET = async (req: NextRequest, res: NextResponse) => {
try {
const client = await clientPromise
const db = client.db("my-portfolio")
const blogs = await db
.collection("blogs")
.find({})
.sort({ order: 1 })
.toArray()
return NextResponse.json(blogs)
} catch (error) {
return NextResponse.json({ error })
}
}Next.js 13 with App Router
In my setup, Next.js offers SSR, simplifies routing, and optimizes performance, making it superior to React alone. Version 13 notably boosts performance, while the App Router streamlines API route handling within the application. For instance, in route.ts, I leverage the MongoDB client promise from db/mongodb.ts to access a 'blogs' collection in the 'my-portfolio' database. The GET handler fetches, sorts, and responds with all blog entries using Next.js NextResponse, eliminating the need for an external RESTful API.