feat: make blog data dynamic
This commit is contained in:
16
app/api/blogs/route.js
Normal file
16
app/api/blogs/route.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import axios from "axios";
|
||||
import { backendUrl } from "@/utils/axios";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const response = await axios.get(`${backendUrl}/blogs/`);
|
||||
return NextResponse.json(response.data);
|
||||
} catch (error) {
|
||||
console.error("Error fetching blogs:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to fetch blog data" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
19
app/api/blogs[slug]/route.js
Normal file
19
app/api/blogs[slug]/route.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import axios from "axios";
|
||||
import { backendUrl } from "@/utils/axios";
|
||||
|
||||
export async function GET(request, { params }) {
|
||||
const { slug } = params;
|
||||
|
||||
try {
|
||||
console.log(`API route fetching blog with slug: ${slug}`);
|
||||
const response = await axios.get(`${backendUrl}/blogs/${slug}/`);
|
||||
return NextResponse.json(response.data);
|
||||
} catch (error) {
|
||||
console.error(`Error fetching blog with slug ${slug}:`, error);
|
||||
return NextResponse.json(
|
||||
{ error: `Failed to fetch blog: ${error.message}` },
|
||||
{ status: error.response?.status || 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user