Using Cloud Code with the Parse Server and Heroku

2020-06-03 07:35发布

问题:

I am trying to understand the new Parse Server and have deployed on Heroku. This went smoothly but what I am struggling with is figuring out how to write server side code (Cloud Code). I've read over the parse server example many times so I must be missing something but I'm very unclear if I should be using Express for something, or how I even begin to include my Cloud Code files. Any help is very much appreciated.

UPDATE:

I found the cloud folder I was just looking in the wrong place. I moved it and index.js to my apps folder on the desktop. I have changed the default code in main.js to my custom code. I have set up index.js with my apps information. The problem now is when I run the app and try to call the cloud code functions I get error invalid function.

回答1:

If you have the parse server example running on heroku you are 90 percent there. Just open the cloud/main.js file and start adding your cloud code. There should be a hello cloud function there as an example.

To use your already created cloud code modules/files you can require them as you have done before on parse.com. The only difference is that the path should now be relative instead of absolute. For example require('cloud/cloudFunctions'); should be require('./cloudFunctions'); if you had a module called cloudFunctions.js in the cloud directory.

Cloud Code works similar to how it did on parse.com and you shouldn't have to think too much about expressjs for simple applications. That said, parse server is using expressjs so yes you are using it.

Parse server is simply a another node module similar to the other thousands available. If you do not have previous experience with nodejs, running parse server can seem complicated. Therefore I would recommend reading about the basics of nodejs before a full migration.



回答2:

I'm using the Bitnami stack on a Google Compute Engine instance and I had a similar problem to yours. To solve it, just navigate to the folder where your server.js file is and create a folder called "cloud". Then create the main.js file inside the cloud folder with the following content:

Parse.Cloud.define('hello', function(req, res) {
  res.success('Hi');
});

Now open the server.js file and find the line containing the path to the cloud code file. Change it to point to you main.js file like this:

This could be any arbitrary folder of your choosing. Now just restart your parse server and call the cloud function:

String result = ParseCloud.callFunction("hello", new HashMap<>());

This is with the Java SDK but should not be much different. The variable result will equal "Hi" if you've used the function from above.