I made a structure myself. But it surely can be improved.
I tried to make my main.js simple. I only added the functions names which will be called outside of cloud code.
// 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);
In file1.js I wrote functions as following.
// 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
}
I exported modules as shown because it makes the code more readable. I can just look at the top of the code and see what functions I can use from this file. You may use the docs export style.
exports.addComment = function(request, response) {
// your code
response.success();
}