我正在为社交移动应用程序使用Parse Cloud Code。我想让云代码具有可伸缩性,但是Parse有一些我必须遵守的规则。其结构如下:
cloud/
main.js
other.js
otherfile/
someother.js
...
...只有main.js是必需的,移动客户端只能调用main.js内部的函数。
在我的客户端,我使用MVC作为架构,但我不确定我应该在云代码中使用哪种架构。我的云代码架构应该是什么样子。
是否有一个我可以使用的通用后端架构?
发布于 2015-04-07 19:37:19
我自己做了一个结构。但它肯定是可以改进的。
我试图使我的main.js变得简单。我只添加了将在云代码之外调用的函数名。
// Include all of the modules
var module1 = require('cloud/folder1/file1.js');
var module2 = require('cloud/folder1/file2.js');
var module3 = require('cloud/folder2/file1.js');
var backgroundjob = require('cloud/backgroundjob/background.js');
Parse.Cloud.job("startBackgroundJob", backgroundjob.startBackgroundJob);
Parse.Cloud.define("do_this_stuff", module1.thisfunction);
Parse.Cloud.define("do_this_stuff2", module1.notthisfunction);
Parse.Cloud.define("do_that_stuff", module2.thatfunction);
Parse.Cloud.define("do_dat_stuff", module3.datfunction);在file1.js中,我编写了如下函数。
// Include libraries
var utils = require("cloud/utils/utils.js");
var _ = require('underscore');
// Export Modules
module.exports = {
thisfunction: function (request, response) {
addComment(request, response);
},
thatfunction: function (request, response) {
getComment(request, response);
},
};
function addComment(request, response) {
// write your code here
var stuff = utils.callThisFunction(param); // This is the usage of another function in another file
response.success("Comment added"); // or error but do not forget this
}
function getComment(request, response) {
// write your code here
response.success("Got Comment"); // or error but do not forget this
}如图所示,我导出了模块,因为它使代码更具可读性。我只需查看代码的顶部,看看我可以从这个文件中使用哪些函数。您可以使用docs export style。
exports.addComment = function(request, response) {
// your code
response.success();
}发布于 2015-04-15 11:45:06
您可以通过在main.js旁边创建一个新模块将代码拆分为模块,比如services.js
并在main.js中要求它
require("cloud/services.js");最后,该文件中定义的所有云函数都可以用于您,就像在main.js中一样。这是因为当您在require中运行该文件中的所有内容时,这实际上意味着您只是将所有这些代码分解到一个单独的文件中。
发布于 2015-04-06 20:27:50
在云代码中,main.js按原样存在。所有云代码函数都位于该单个文件中。没有任何调制或附加架构。
Parse.Cloud.run(名称、数据、选项)是调用Parse函数的唯一方法。
R
https://stackoverflow.com/questions/28129613
复制相似问题