Next.js Discord

Discord Forum

Using a file in a serverless function

Unanswered
ani posted this in #help-forum
Open in Discord
aniOP
Look I have keys folder on root of my project. The serverless function is in pages/api/callback.ts. I have some json files in the keys folder. I want to access them on the function. So how can i access them?

8 Replies

@ani Look I have `keys` folder on root of my project. The serverless function is in `pages/api/callback.ts`. I have some json files in the keys folder. I want to access them on the function. So how can i access them?
import { NextApiRequest, NextApiResponse } from "next";
import fs from "fs";
import path from "path";

export default async function handler(
  _req: NextApiRequest,
  res: NextApiResponse
) {
  const data = await fs.promises.readFile(
    path.join(process.cwd(), "keys", "data.json"),
    "utf-8"
  );

  res.status(200).json({ data: JSON.parse(data) });
}
no
@Ray does it work for you?
aniOP
yes it did
thank u
@ani yes it did
cool, can you please mark the solution