Reputation: 188
Is there any reason I'm getting this error when running parse server locally, but not when hosted on Heroku? Status:100 Message:XMLHttpRequest failed: "Unable to connect to the Parse API"
I have mongodb installed and running locally. I also have a .env file that contains APP_ID
and MASTER_KEY
that gets pulled in with dotenv locally.
Here's an abbreviated example of what I'm trying to do...
var express = require('express');
var bodyParser = require('body-parser');
var dotenv = require('dotenv').config({silent: true});
var ParseServer = require('parse-server').ParseServer;
var Parse = require('parse/node');
// Set up the parse server
var databaseUri = process.env.DATABASE_URI || process.env.MONGOLAB_URI;
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myAppId',
masterKey: process.env.MASTER_KEY || ''
});
// Set up express
var app = express();
app.use(express.static(__dirname + '/public'));
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Initialize parse so that I can use in my routes and controllers
Parse.initialize(process.env.APP_ID,'unused');
Parse.serverURL = process.env.SERVER_URI || 'http://locahost:1337/parse';
// Routes
app.get('/', function(req, res) {
res.status(200).send('[form goes here that sends POST to "/sign-up"]');
});
app.post('/sign-up', function(req, res) {
var user = new Parse.User();
user.set("email", req.body.inputEmail);
user.set("password", req.body.inputPassword);
user.signUp(null, {
success: function(user) {
res.status(200).send('Signup Success!');
},
error: function(user, error) {
res.status(200).send('Signup Failed');
}
});
});
var port = process.env.PORT || 1337;
app.listen(port);
Upvotes: 1
Views: 2312
Reputation: 8151
The error Unable to connect to the Parse API
implies that the Parse sdk can't connect to the given serverURL. In your case it seems to be a typo: http://locahost:1337/parse should be http://localhost:1337/parse?
You should also remove the var Parse = require('parse/node');
and the custom initialization as the parse sdk will be initialized in Parse Server and Parse
will then be available globally. Take a look at the usage example in the readme. Your code should look something like this:
var express = require('express');
var bodyParser = require('body-parser');
var dotenv = require('dotenv').config({silent: true});
var ParseServer = require('parse-server').ParseServer;
// Set up the parse server
var databaseUri = process.env.DATABASE_URI || process.env.MONGOLAB_URI;
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myAppId',
masterKey: process.env.MASTER_KEY || '',
serverURL: process.env.SERVER_URI || 'http://localhost:1337/parse'
});
// Set up express
var app = express();
app.use(express.static(__dirname + '/public'));
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Routes
app.post('/sign-up', function(req, res) {
var user = new Parse.User(); // Parse available as a global variable
});
var port = process.env.PORT || 1337;
app.listen(port);
Upvotes: 2