fix: add __appRoot to fix bundler messing up __dirname for nested files

This commit is contained in:
William Oldham 2025-01-09 21:06:21 +00:00
parent fec123d838
commit d56b9498ea
3 changed files with 9 additions and 3 deletions

4
src/app-root.ts Normal file
View File

@ -0,0 +1,4 @@
// ! Do not move this file, it must be in the root of the project for __dirname to resolve correctly
// * Node.js does not have a built-in way to get the root directory of the project, so we need to set it manually
// * When using a bundler, __dirname will not work as expected in nested files so we need a consistent way to get the root directory
export const __appRoot = __dirname;

View File

@ -2,12 +2,13 @@ import path from 'node:path';
import express from 'express';
import subdomain from 'express-subdomain';
import { fileErrCallback } from '@/util';
import { __appRoot } from '@/app-root';
const npdi = express.Router();
npdi.get('/p01/data/1/:titleHash/:dataID/:fileHash', (request, response) => {
const { titleHash, fileHash } = request.params;
const contentPath = path.normalize(`${__dirname}/../../cdn/content/encrypted/${titleHash}/${fileHash}`);
const contentPath = path.normalize(`${__appRoot}../cdn/content/encrypted/${titleHash}/${fileHash}`);
response.sendFile(contentPath, {
headers: {

View File

@ -2,12 +2,13 @@ import path from 'node:path';
import express from 'express';
import subdomain from 'express-subdomain';
import { fileErrCallback } from '@/util';
import { __appRoot } from '@/app-root';
const npts = express.Router();
npts.get('/p01/tasksheet/:id/:hash/:fileName', (request, response) => {
const { id, hash, fileName } = request.params;
const tasksheetPath = path.normalize(`${__dirname}/../../cdn/tasksheet/${id}/${hash}/${fileName}`);
const tasksheetPath = path.normalize(`${__appRoot}/../cdn/tasksheet/${id}/${hash}/${fileName}`);
response.sendFile(tasksheetPath, {
headers: {
@ -18,7 +19,7 @@ npts.get('/p01/tasksheet/:id/:hash/:fileName', (request, response) => {
npts.get('/p01/tasksheet/:id/:hash/:subfolder/:fileName', (request, response) => {
const { id, hash, subfolder, fileName } = request.params;
const tasksheetPath = path.normalize(`${__dirname}/../../cdn/tasksheet/${id}/${hash}/_subfolder/${subfolder}/${fileName}`);
const tasksheetPath = path.normalize(`${__appRoot}/../cdn/tasksheet/${id}/${hash}/_subfolder/${subfolder}/${fileName}`);
response.sendFile(tasksheetPath, {
headers: {