gpt4 book ai didi

javascript - 访问根公共(public)目录nodejs

转载 作者:行者123 更新时间:2023-11-30 00:17:49 25 4
gpt4 key购买 nike

有没有一种方法可以在不使用 ../../../ 的情况下访问根公共(public)目录?下面的代码工作正常,但只是觉得我访问我的公共(public)文件夹的方式有点困惑。

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

最佳答案

方法是使用 app-root-path .它是一个模块,可帮助您从应用程序的任何位置访问应用程序的根路径,而无需求助于相对路径。

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

关于javascript - 访问根公共(public)目录nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34173686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com