SailsJS如何在项目的根级指定文件path

我有一个SailsJS应用程序,我需要引用位于我的项目根目录下的htpasswd文件:

var auth = require('http-auth'); var basic = auth.basic({ authRealm: "Admin Panel", authFile: 'htpasswd', // <-- how do I specify the path to this file ? authType: 'basic' }); module.exports = function(req, res, next) { basic.apply(req, res, function(username) { if(!username) { return res.serverError(403, 'You are not authorized'); } next(); })(req, res); } 

我曾尝试使用:

 authFile: '/htpasswd' 

以及:

 authFile: '~/htpasswd' 

两者都行不通。

更新

Hurmm ….它似乎不是代码有错误,不知何故,我的Sailsjs应用程序找不到htpasswd模块。

我做了:

 sudo npm install -g htpasswd 

我也用htpasswd命令行来生成htpasswd文件… …我的项目设置错误…

我的控制台错误说:

 Error: Cannot find module 'htpasswd' at Function.Module._resolveFilename (module.js:336:15) at Function.Module._load (module.js:278:25) at Module.require (module.js:365:17) at require (module.js:384:17) at Object.<anonymous> (/Users/MacUser/SailsProjects/SecurePanel/node_modules/http-auth/gensrc/auth/basic.js:11:14) 

使用计算为当前文件path的本机__dirnamevariables。

看看它使用console.log(__dirname) 。 然后你可以这样做:

 var basic = auth.basic({ realm: "Admin Panel", file: __dirname + "/path_from_current_file_to/htpasswd" }); 

所以如果根目录是从这个脚本所在的文件夹中,你可以这样做

__dirname + "/../htpasswd"