parse-serverparse-javascript-sdk

Parse-Server - Cannot get data from DB on a remote machine


I am using Parse-Server and I have softwares developed on C# and Swift getting data from this server remotely using the appropriated SKDs to each programming language without any problems.

But now I need to get data using a browser and I am having issues to get it from a remote computer. I just can get it using localhost on the computer where the Parse-Server is installed.

I am using the parse sdk for javascript.

Here is my code (index.html):

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Teste</title>

    <script src="https://unpkg.com/parse/dist/parse.min.js"></script>
    <script>
        function executaScript() {
            console.log("Script")

            Parse.initialize("HG", "HGJSKey", "HGMasterKey");
            Parse.serverURL = "http://localhost:1337/parse";

            var etapasInternacao = Parse.Object.extend("EtapaInternacao");
            var innerQuery = new Parse.Query(etapasInternacao);
            innerQuery.notEqualTo("objectId", "o0rudDQzng");

            var interns = Parse.Object.extend("Internacao");
            var query = new Parse.Query(interns);
            query.include("paciente");
            query.include("etapa");
            query.ascending("numeroQuarto");
            query.matchesQuery("etapa", innerQuery);

            query.find().then((resultado) => {

                this.internacoes = []
                for(var indice in resultado) {
                    console.log(resultado[indice].get("paciente").get("nomeCompleto"))
                }
            }, (error) =>  {
                // error is an instance of parse.error.
                alert(error.message)
            });
        }
        document.onload = executaScript()
    </script>

  </head>
  <body>
    <h1>Script</h1>
  </body>
</html>

And here is the Parse-Server inicial file (index.js)

// Example express application adding the parse-server module to expose Parse
// compatible API routes.

var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var path = require('path');
const resolve = require('path').resolve;

var api = new ParseServer({
  databaseURI: 'mongodb://localhost:27017/HGDB',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID || 'HG',
  masterKey: process.env.MASTER_KEY || 'HGMasterKey', //Add your master key here. Keep it secret!
  serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',  // Don't forget to change to https if needed

  clientKey: 'HGKey',
  javascriptKey: 'HGJSKey',
  restAPIKey: 'HGRestKey',
  dotNetKey: 'HGDotNetKey',
  fileKey: 'HGFileKey',

  liveQuery: {
    classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
  }
});
// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey

var app = express();

// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));

// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);

// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
  res.status(200).send('I dream of being a website.  Please star the parse-server repo on GitHub!');
});

// There will be a test page available on the /test path of your server url
// Remove this before launching your app
app.get('/test', function(req, res) {
  res.sendFile(path.join(__dirname, '/public/test.html'));
});

var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
    console.log('parse-server-example running on port ' + port + '.');
});

// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);

And here are the Error I receive from the browsers:

Google Chrome:

POST http://localhost:1337/parse/classes/Internacao net::ERR_CONNECTION_REFUSED

Mozila Firefox:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:1337/parse/classes/Internacao. (Reason: CORS request did not succeed).

I have downloaded cors() and used it just after the app declaration and it still doesn't work.

[...]
var app = express();
app.use(cors());
[...]

I have tried creating routes and cors options and nothing changes.


Solution

  • I changed this line

    Parse.serverURL = "http://localhost:1337/parse";

    to

    Parse.serverURL = "http://[SERVER-IP]:1337/parse";

    and it is working now.