Using Cloud Code with the Parse Server and Heroku
Asked Answered
F

2

8

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.

Fang answered 4/3, 2016 at 3:58 Comment(0)
A
8

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.

Atharvaveda answered 4/3, 2016 at 18:48 Comment(7)
Thanks for the great answer! I still have a few questions. I used the "Deploy to Heroku" button and don't have a main.js file. Could you point me in the direction of a the correct way to deploy to heroku that would include the main.js file? I will definitely read about nodejs.Fang
Aha! Did you run heroku git:clone -a yourAppName to get your files locally after using the deploy button? If so you should have gotten a cloud directory with a main.js file? To continue making updates you can read about deployment on heroku here devcenter.heroku.com/articles/deploying-nodejsAtharvaveda
Thanks! Yeah I did but where is the main.js file suposed to appear? Because I haven't been able to seem to find it.Fang
Strange! Did you get the cloud folder?Atharvaveda
It should be in your root directory? You can change the name however, as long as you update the path in your parse server initialize code.Atharvaveda
How about the resource limits? Legacy Parse.com has a limit of 3sec for before/after save hooks. Is that the case with Parse Server?Mortgagor
Nope. No such limits.Atharvaveda
S
1

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:

server.js config file

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.

Sacha answered 3/8, 2016 at 11:25 Comment(2)
I am using the Bitnami Stack also, but as soon as I change the cloud parameter from ./node_modules/parse-server/lib/cloud-code/Parse.Cloud.js to ./cloud/main.js my website stops running - I try to append my cloud code to the Parse.Cloud.js file also but even then it stops working. I need to migrate my own cloud code to the server, how do I do that? Thank you.Lippmann
@Lippmann Please check the parse logs and post any errors messages. I found most errors can be solved easily this way. When you stop the server it'll tell you where the log is located.Sacha

© 2022 - 2024 — McMap. All rights reserved.