NuxtN
Nuxtβ€’12mo agoβ€’
11 replies
π”ͺπ”Ÿπ”©π”’

Server read data from public

Hello,
In server/api I have data script, that reads file from public, and returns json
import fs from 'fs';
import {parse} from 'smol-toml';
import path from 'path';
import url from 'url';

export default defineEventHandler((event) => {
  try {
    const dataRaw = fs.readFileSync(path.relative(process.cwd(), './public/personal/main.toml'));
    const data = parse(dataRaw.toString());
    return data;
  } catch (e) {
    console.log(e);
    return {}
  }
})

When I build and run it locally, everything is fine; but when I try to deploy it with netlify I get empty response from api/data.
Most likely it is due to current working directory (process.cwd()) being outside of dist folder, however I have no idea, how else I can load that data, as relative path to public differs between dev and build.
Was this page helpful?