[SOLVED]
I have created a dynamic API route in NextJS that, if given a filename, the server will read the file (.md or .txt file) under the content directory, convert it into string and returns it. The API works as intended in local environment, however, the deployed website in Vercel doesn't work properly for this API route.
That API route, for a file that exists in the content directory, returns 500 internal server error.
The console error is:
Failed to load resource: the server responded with a status of 500 ()
Checking the vercel logs for the route, showed this:
ERROR Error: ENOENT: no such file or directory, open 'content/phase1_section1.md'
I think the path.join() is not working for me. It is not adding the actual domain name of the app in front of the content part. How can I resolve this?
And, here's the code
import { readFileSync } from 'fs';
import path from "path";
export default function courseHandler({ query: { slug } }, res) {
const fullPath = path.join('./content/', slug);
const content = readFileSync(fullPath, 'utf8');
if (content) {
res.status(200).json({
content: content
});
} else {
res.status(404).json({ message: `Course with slug: ${slug} not found.` })
}
}
content/phase1_section1.mdon the environment where your API is running?