nuxt.config.ts
(config upload path)
export default defineNuxtConfig({
telemetry: false,
runtimeConfig: {
paths: {
upload: path.join(process.cwd(), '../upload'), //fs.realpathSync( '../upload') --> will be an error, if it does not exist
}
},
// ...
});
/server/utils/folderTools.ts
(exports will be loaded in all server/**/*.ts)
export async function checkOrCreateUploadFolder(folderName:string) {
let err:any = false;
const UPLOAD_PATH = path.join(useRuntimeConfig().paths.upload, folderName);
let exists = await fs.realpath(UPLOAD_PATH).catch(_ => null);
if (!exists) {
fs.mkdir(UPLOAD_PATH, {recursive: true}).catch( e => err = {msg: e.msg});
exists = await fs.realpath(UPLOAD_PATH).catch(_ => null);
}
return {err, path: exists};
}