From 2384b30a244c7d6477e54de5385fe7f1cc62d43a Mon Sep 17 00:00:00 2001 From: "Thomas F. K. Jorna" Date: Fri, 8 Oct 2021 23:39:37 +0200 Subject: feat(preview): proper file preview with api routing --- pages/api/notes/[[...slug]].ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 pages/api/notes/[[...slug]].ts (limited to 'pages/api/notes') diff --git a/pages/api/notes/[[...slug]].ts b/pages/api/notes/[[...slug]].ts new file mode 100644 index 0000000..b0df82a --- /dev/null +++ b/pages/api/notes/[[...slug]].ts @@ -0,0 +1,17 @@ +import fs from 'fs' + +export default async function handler(req: any, res: any) { + const { slug } = req.query + const stuff = slug.join('') + console.log(stuff) + const uri = decodeURIComponent(slug) + const prefix = uri.includes('\\') ? '' : '/' + const path = `${uri}` + try { + const text = fs.readFileSync(`${path}`, { encoding: 'utf-8' }) + res.end(`${text}`) + } catch (e) { + res.end(`Oopsie Whoopsie! We did a fucky wucky!`) + console.log(e) + } +} -- cgit v1.2.3