Skip to content

Instantly share code, notes, and snippets.

@airicbear
Created April 28, 2023 03:48
Show Gist options
  • Save airicbear/250634bb7a3020e77e2a14030b31b53b to your computer and use it in GitHub Desktop.
Save airicbear/250634bb7a3020e77e2a14030b31b53b to your computer and use it in GitHub Desktop.
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