Eag*_*gle 5 javascript node.js sails.js
我是 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]));
}
});
}
Run Code Online (Sandbox Code Playgroud)
但是我需要经常使用那个arrowDBAppvar 来创建、更新、删除数据库中的对象,所以我认为最好的方法是在启动脚本中初始化它app.js并在应用程序中共享。
我试过了,但我无法将它存储在sailsvar 中,似乎在执行 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");
})();
Run Code Online (Sandbox Code Playgroud)
不打印“HI”和“Finish”。如果我尝试sails.arrowDBApp在另一个控制器中使用,它是未定义的。
欢迎提供提示。
app.js除非确实需要,否则不建议进行修改。
保存所有配置信息(例如APP_KEY)的常用空间位于项目根目录的config目录中。
一次性初始化(例如 ArrowDB 初始化)可以添加到config/bootstrap.js。
更新
在 config/arrowdb.js(你需要自己创建这个文件):
module.exports.arrowdb = {
APP_KEY: 'yourappkey',
ArrowDBApp: null
};
Run Code Online (Sandbox Code Playgroud)
在 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
};
Run Code Online (Sandbox Code Playgroud)
在您的控制器中:
'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.
}
Run Code Online (Sandbox Code Playgroud)
config/bootstrap.js在帆升起之前初始化一些东西。有时如果我们想把一些东西放在global变量中,这种方法很好用,比如Promise用Bluebird Promise.api/services在代码(控制器、模型等)中放置一些您将经常使用的方法或其他东西,例如Mail Service在您的应用程序中处理发送电子邮件的 。config文件夹在sails.config[something]. 它可以是一个对象、函数或任何可配置的对象,例如将 Twitter API 密钥放置为使用 Twitter REST API。为了实现您想要的,我将尝试使用 service 和 bootstrap.js。试试这个例子。
api/services/ArrowDBService.js加上这个代码:
var ArrowDB = require('arrowdb'),
arrowDBApp = new ArrowDB('<App Key>');
module.exports = {
arrowDBApp : arrowDBApp,
login : function (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]));
}
});
}
};
Run Code Online (Sandbox Code Playgroud)现在您可以通过sails.services.arrowdbservice.login(req,res)或简单地使用它ArrowDBService.login(req,res)(注意区分大小写的事情)。由于我不了解 ArrowDB,因此您可以自行探索login示例提供的方法。
| 归档时间: |
|
| 查看次数: |
354 次 |
| 最近记录: |