我是 Sails 的新手,不知道将对象的初始化放在哪里才能在所有应用程序中保持唯一。阅读文档后,我假设我可以将它放在全局 sails
对象中,但不确定是否是更好的地方。
我正在使用新的 Appcelerator ArrowDB 来存储我的用户和对象。文档讨论了通过 APP_KEY 声明适当的变量并使用它。
var ArrowDB = require('arrowdb'),
arrowDBApp = new ArrowDB('<App Key>');
function login(req, res) {
var data = {
login: req.body.username,
password: req.body.password,
// the req and res parameters are optional
req: req,
res: res
};
arrowDBApp.usersLogin(data, function(err, result) {
if (err) {
console.error("Login error:" + (err.message || result.reason));
} else {
console.log("Login successful!");
console.log("UserInfo: " + JSON.stringify(result.body.response.users[0]));
}
});
}
但是我需要不断地使用 arrowDBApp
var 来创建、更新、删除数据库中的对象,所以我认为最好的方法是在启动脚本 app 中初始化它。 js
并在整个应用程序中共享。
我试过了,但我无法将它存储在 sails
var 中,似乎在执行 sails.lift() 之前此 var 不可用(或丢失其配置)。
此代码(app.js 文件)在控制台中不显示任何内容:
// Ensure we're in the project directory, so relative paths work as expected
// no matter where we actually lift from.
process.chdir(__dirname);
// Ensure a "sails" can be located:
(function() {
var sails;
try {
sails = require('sails');
} catch (e) {
console.error('To run an app using `node app.js`, you usually need to have a version of `sails` installed in the same directory as your app.');
console.error('To do that, run `npm install sails`');
console.error('');
console.error('Alternatively, if you have sails installed globally (i.e. you did `npm install -g sails`), you can use `sails lift`.');
console.error('When you run `sails lift`, your app will still use a local `./node_modules/sails` dependency if it exists,');
console.error('but if it doesn\'t, the app will run with the global sails instead!');
return;
}
// Try to get `rc` dependency
var rc;
try {
rc = require('rc');
} catch (e0) {
try {
rc = require('sails/node_modules/rc');
} catch (e1) {
console.error('Could not find dependency: `rc`.');
console.error('Your `.sailsrc` file(s) will be ignored.');
console.error('To resolve this, run:');
console.error('npm install rc --save');
rc = function () { return {}; };
}
}
// My own code
var APP_KEY = 'mykey';
var ArrowDB = require('arrowdb');
sails.arrowDBApp = new ArrowDB(APP_KEY);
console.log("Hi" + JSON.stringify(sails));
// Start server
sails.lift(rc('sails'));
console.log("Finish");
})();
没有打印“HI”和“Finish”。如果我尝试在另一个 Controller 中使用 sails.arrowDBApp
,它是未定义的。
欢迎提示。
最佳答案
除非确实需要,否则不建议修改 app.js
。
通常用于保存所有配置信息(例如 APP_KEY
)的空间在 config 中项目根目录中的目录。
可以将一次性初始化(例如 ArrowDB 初始化)添加到 config/bootstrap.js .
更新
在config/arrowdb.js中(需要自己创建这个文件):
module.exports.arrowdb = {
APP_KEY: 'yourappkey',
ArrowDBApp: null
};
在 config/bootstrap.js 中:
var ArrowDB = require('arrowdb');
module.exports.bootstrap = function(next){
sails.config.arrowdb['ArrowDBApp'] = new ArrowDB(sails.config.arrowdb['APP_KEY']);
next(); // Don't forget to add this
};
在你的 Controller 中:
'task': function(req, res, next) {
sails.config.arrowdb['ArrowDBApp'].usersLogin(...);
// and so on.
// You could also add something like
// var ADB = sails.config.arrowdb['ArrowDBApp'];
// at the top in case you need to use it on and on.
}
关于javascript - 在 SailsJS 中设置初始化的更好地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31247342/