调用用户signUp时,本地运行的解析服务器出错

时间:2016-02-18 07:23:26

标签: node.js express heroku parse-platform parse-server

在本地运行解析服务器时是否有任何原因导致此错误,但在Heroku上托管时却没有? 状态:100消息:XMLHttpRequest失败:“无法连接到Parse API”

我已在本地安装和运行mongodb。我还有一个包含APP_IDMASTER_KEY的.env文件,该文件会在本地使用dotenv。

这是我正在尝试做的缩写示例......

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);

1 个答案:

答案 0 :(得分:2)

错误Unable to connect to the Parse API意味着Parse sdk无法连接到给定的serverURL。在你的情况下,它似乎是一个错字:http://locahost:1337/parse应该是http://localhost:1337/parse

您还应该删除var Parse = require('parse/node');和自定义初始化,因为解析sdk将在解析服务器中初始化,然后Parse将全局可用。请查看自述文件中的usage示例。您的代码应如下所示:

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);