Parse-server: Is there a reliable way to tell from Cloud Code if it is running on a parse-server vs hosted Parse.com?

Created on 25 Apr 2016  路  3Comments  路  Source: parse-community/parse-server

I've been doing a variant of the following:

// main.js
var IS_PARSE_SERVER;
try {
  require('cloud/some-valid-module-in-hosted-parse');
  IS_PARSE_SERVER = false;
} catch (e) {
  IS_PARSE_SERVER = true;
}

but I realized adding a flag to the global Parse object should work the same way.

// app.js
var express = require('express');
var ParseServer = require('parse-server').ParseServer;

// !!!
Parse.IS_PARSE_SERVER = true;

var api = new ParseServer({...});
var app = express();
app.use('/parse', api);
app.listen(1337);

// main.js
var IS_PARSE_SERVER = Parse.IS_PARSE_SERVER;
  1. Which method is preferred: the first one, second one, or neither?
  2. If the second one is preferred, does it make sense to add it into this project (as opposed to having it done on a per-app basis)?

Most helpful comment

For a more reliable method I'd recommend adding an environment variable like YUZEH_APP_PARSE_SERVER to the server you are running Parse Server on and checking that. That method will be guaranteed to never break due to changes in Parse Server, Parse.com, or the Parse JS SDK.

All 3 comments

If you're doing this to run the same code on both parse.com and parse-server, the second approach seems better. I have been thinking recently about this middle-state where you have 2 api servers, 2 sets of cloud code, and how we can improve this.... but this is a decent solution in my book.

There are two methods that are not available on Parse Server:

  • Parse.User.current()
  • Parse.Cloud.useMasterKey()

You could check for the existence of these to determine if you're on Parse hosted Cloud Code. Of course this would only work as long as Parse Server does not support these methods.

For a more reliable method I'd recommend adding an environment variable like YUZEH_APP_PARSE_SERVER to the server you are running Parse Server on and checking that. That method will be guaranteed to never break due to changes in Parse Server, Parse.com, or the Parse JS SDK.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carjo422 picture carjo422  路  3Comments

LtrDan picture LtrDan  路  4Comments

sanergulec picture sanergulec  路  4Comments

dpaid picture dpaid  路  3Comments

AmbroiseCollon picture AmbroiseCollon  路  4Comments