访问根公共目录nodejs

accessing root public directory nodejs

本文关键字:nodejs 访问      更新时间:2023-09-26

有没有一种方法可以在不使用../../../的情况下访问根公共目录。下面的代码运行良好,但我只是觉得我访问公用文件夹的方式有点混乱。

router.get('/', function(req, res) {
    res.sendFile('index.html', { root: path.join(__dirname, '../../public')   });
});

应用程序结构:

/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

即将使用应用程序根路径。它是一个模块,可以帮助您从应用程序中的任何位置访问应用程序的根路径,而无需求助于相对路径。

var appRoot = require('app-root-path');
router.get('/', function(req, res) {
    res.sendFile('index.html', { root: path.join(__dirname, appRoot + '/public')   });
});