news list now can load local json file
This commit is contained in:
parent
5c0a5128ac
commit
14294f8a14
9 changed files with 17 additions and 3 deletions
16
ui/app/api/news/route.ts
Normal file
16
ui/app/api/news/route.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import fs from "node:fs/promises";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
export async function GET() {
|
||||||
|
try {
|
||||||
|
const dataDirectory = path.join(process.cwd(), "public", "data");
|
||||||
|
const filePath = path.join(dataDirectory, "index.json");
|
||||||
|
const fileContents = await fs.readFile(filePath, "utf8");
|
||||||
|
const data = JSON.parse(fileContents);
|
||||||
|
return NextResponse.json(data);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error reading news data:", error);
|
||||||
|
return NextResponse.json({ error: "Failed to load news data" }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,9 +19,7 @@ const NewsPage = () => {
|
||||||
const fetchNews = async () => {
|
const fetchNews = async () => {
|
||||||
try {
|
try {
|
||||||
console.log("Fetching news...");
|
console.log("Fetching news...");
|
||||||
const response = await fetch(
|
const response = await fetch("/api/news");
|
||||||
"https://raw.githubusercontent.com/newspedia-crew/newspedia-web/intern-change/public/data/index.json",
|
|
||||||
);
|
|
||||||
console.log("Response status:", response.status);
|
console.log("Response status:", response.status);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! status: ${response.status}`);
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue