Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing root public directory nodejs

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
like image 486
ShankarGuru Avatar asked Oct 20 '25 10:10

ShankarGuru


1 Answers

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')   });
});
like image 95
Alexandr Lazarev Avatar answered Oct 21 '25 22:10

Alexandr Lazarev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!