Update route.ts

This commit is contained in:
Vaica committed 2026-06-21 22:44:05 +08:00
1 parent dc796c2b66
commit 4121a15587
1 file changed
+12
+12
View File
@@ -1,9 +1,18 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { listPosts, createPost, getPostCount } from "@/lib/posts"; import { listPosts, createPost, getPostCount } from "@/lib/posts";
import { cleanExpired } from "@/lib/recycle"; import { cleanExpired } from "@/lib/recycle";
import { CONTENT_DIR, BLOG_ROOT } from "@/lib/config";
import fs from "fs";
export async function GET(request: NextRequest) { export async function GET(request: NextRequest) {
try { try {
// Debug logging
console.log("[api/posts] BLOG_ROOT:", BLOG_ROOT);
console.log("[api/posts] CONTENT_DIR:", CONTENT_DIR);
console.log("[api/posts] cwd:", process.cwd());
console.log("[api/posts] env BLOG_ROOT:", process.env.BLOG_ROOT);
console.log("[api/posts] CONTENT_DIR exists:", fs.existsSync(CONTENT_DIR));
cleanExpired(); // housekeeping on each request cleanExpired(); // housekeeping on each request
const { searchParams } = request.nextUrl; const { searchParams } = request.nextUrl;
@@ -15,8 +24,11 @@ export async function GET(request: NextRequest) {
const result = listPosts({ search, page, perPage, includeDrafts }); const result = listPosts({ search, page, perPage, includeDrafts });
const totalCount = getPostCount(); const totalCount = getPostCount();
console.log("[api/posts] result:", result.total, "posts");
return NextResponse.json({ ...result, totalCount }); return NextResponse.json({ ...result, totalCount });
} catch (error: unknown) { } catch (error: unknown) {
console.error("[api/posts] error:", error);
return NextResponse.json( return NextResponse.json(
{ error: error instanceof Error ? error.message : "Unknown error" }, { error: error instanceof Error ? error.message : "Unknown error" },
{ status: 500 } { status: 500 }