Im trying to put some JSON files I have in my next apps directory on a api so I can use the data in the files in my pages.
This is what im currently doing
<code>import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { promises as fs } from "node:fs";
export async function GET(req: NextRequest) {
const filePath = new URL("agile-app/src/scripts/data/2024-03-08.json")
const fileBuffer = await fs.readFile(filePath);
const json = JSON.parse(fileBuffer.toString());
return NextResponse.json(json);
}</code>
<code>import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { promises as fs } from "node:fs";
export async function GET(req: NextRequest) {
const filePath = new URL("agile-app/src/scripts/data/2024-03-08.json")
const fileBuffer = await fs.readFile(filePath);
const json = JSON.parse(fileBuffer.toString());
return NextResponse.json(json);
}</code>
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { promises as fs } from "node:fs";
export async function GET(req: NextRequest) {
const filePath = new URL("agile-app/src/scripts/data/2024-03-08.json")
const fileBuffer = await fs.readFile(filePath);
const json = JSON.parse(fileBuffer.toString());
return NextResponse.json(json);
}
And the fetching method
<code>export async function getCrimeData() {
const response = await fetch("http://localhost:3000/api/json", {
cache: "no-cache",
});
const data = await response.json();
let fetchedCrimeData: CrimeData[] = JSON.parse(data);
return fetchedCrimeData;
}</code>
<code>export async function getCrimeData() {
const response = await fetch("http://localhost:3000/api/json", {
cache: "no-cache",
});
const data = await response.json();
let fetchedCrimeData: CrimeData[] = JSON.parse(data);
return fetchedCrimeData;
}</code>
export async function getCrimeData() {
const response = await fetch("http://localhost:3000/api/json", {
cache: "no-cache",
});
const data = await response.json();
let fetchedCrimeData: CrimeData[] = JSON.parse(data);
return fetchedCrimeData;
}
But I keep getting the error SyntaxError: The string did not match the expected pattern.
I cant use getServerSideProperties or getStaticProperties for this specific task.