Is there a way to access the root public directory without using ../../../
something like this. The below code works fine, but just thought the way i am accessing my public folder is bit messier.
router.get('/', function(req, res) {
res.sendFile('index.html', { root: path.join(__dirname, '../../public') });
});
Application structure:
/app
--node-modules
--public
--app //contains all angularjs related files
--assets //contains images and js libraries
--index.html
--server
--routes
--index.js //this holds all the route related code
On way is to use app-root-path. It is a module that helps you access your application's root path from anywhere in the application without resorting to relative paths.
var appRoot = require('app-root-path');
router.get('/', function(req, res) {
res.sendFile('index.html', { root: path.join(__dirname, appRoot + '/public') });
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With