我正在使用 Parse Cloud Code用于社交移动应用程序。我想让云代码可扩展,但 Parse 有一些我必须遵守的规则。结构如下:
cloud/
main.js
other.js
otherfile/
someother.js
...
...
只有 main.js 是必需的,移动客户端只能调用 main.js 中的函数。
在我的客户中,我使用 MVC 作为架构,但我不确定应该在我的云代码中使用哪种架构。我的云代码架构应该如何。
是否有我可以使用的通用后端架构?
最佳答案
我自己做了一个结构。但它肯定可以改进。
我试图让我的 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();
}
关于architecture - 解析云代码结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28129613/