Created
April 28, 2023 03:48
-
-
Save airicbear/250634bb7a3020e77e2a14030b31b53b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from "fs"; | |
import { NextApiRequest, NextApiResponse } from "next"; | |
import path from "path"; | |
type Data = { | |
files: string[]; | |
}; | |
export default function handler( | |
req: NextApiRequest, | |
res: NextApiResponse<Data> | |
) { | |
const { dir } = req.query; | |
if (dir) { | |
const dirPath = path.join(process.cwd(), "public", dir as string); | |
try { | |
const files = fs.readdirSync(dirPath); | |
res.status(200).json({ files }); | |
} catch (error) { | |
console.error(error); | |
res.status(500).json({ files: [] }); | |
} | |
} else { | |
res.status(500).json({ files: [] }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment