I have a sort of this kind of folder structure for my node.js Express application:
/app
/app_api
/app_srv
/routes
/uri_a
index.js
/uri_b
index.js
/...
index.js
/uploads
/views
/...
app.js
Abstractly
Each /uri_x/index.js is exported as an express.router() module and define the middleware to handle the http methods for that specific uri
The /routes/index.js is also exported as an express.Router() that import the routers from each /uri_x/index.js and set the use for that specific uri
app.js imports the \routes router and set it as the route handler for the application routes i.e. app.use('/', app_router)
/routes/uri_a/index.js
import express from 'express';
const appa = module.exports = express.Router();
appa.get('uri_a', (req, res, next) => {
...
};
/routes/index.js
import express from 'express';
import appa from './a_uri';
const app_router = module.exports = express.Router();
app_router.use('/a_uri', appa);
app_router.use('/', (req, res, next) => {
//render start page
};
app.js
import express from 'express';
import path from 'path';
// ...
import app_router from './app_srv/routes';
// ...
/** here is where is going to be the to be or not of the question **/
app.set('views', path.join(__dirname), 'app_srv' ,'views');
app.set('view engine', 'pug');
// ...
app.use('/', app_routers);
// ...
Everything seam to work seamlessly excepto for that the __dirname changes for each uri_x location loosing the path for folders set in app.js with app.set as is the case for /views (and also /uploads).
I guess this happens because each uri sub folder is acting as a sub application which _dirname becomes e.g. c:\app\app_srv\routes\uri_x
It has took a lot of effort to get here and now, before I start changing the application folder structure, repeating the path set in each module, or change to absolute paths, while it is plausible that other solution exist which I am not being able to find;
please...