![](/img/trans.png)
[英]FetchError: request to http://localhost:1337/api/products failed, reason: connect ECONNREFUSED 127.0.0.1:1337
[英]Express with ParseServer - Failed to connect to <ip> port 1337, connection timeout
我在centos 7上的自定義服務器上配置了解析服務器。當從這個服務器命令提示符運行curl命令以測試其工作正常時它工作正常,但是當嘗試使用curl從我的本地系統終端調用它時它給出連接超時。 我也嘗試使用我的php sdk代碼。
這是解析服務器index.js文件:---
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var path = require('path');
var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;
if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}
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 || '', //Add your master key here. Keep it secret!
serverURL: process.env.SERVER_URL || 'http://<ip addr>:1337/parse', // Don't forget to change to https if needed
liveQuery: {
classNames: ["Employee"] // List of classes to support for query subscriptions, this database collections directly used at client end
}
});
// 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'));
});
// Web endpoints
//app.get('/', homeController.index);
//app.get('/about', aboutController.index);
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);
我的Php代碼: -
<?php
require 'autoload.php';
use Parse\ParseClient;
use Parse\ParseObject;
use Parse\ParseQuery;
//use Parse\ParseACL;
//use Parse\ParsePush;
//use Parse\ParseUser;
//use Parse\ParseInstallation;
use Parse\ParseException;
//use Parse\ParseAnalytics;
//use Parse\ParseFile;
use Parse\ParseCloud;
try {
ParseClient::initialize("myAppId", "", "", true);
// Users of Parse Server will need to point ParseClient at their remote URL and Mount Point:
ParseClient::setServerURL('http://<ip address>:1337', 'parse');
//Save data start
$gameScore = new ParseObject("GameScore");
$gameScore->set("score", 1337);
$gameScore->set("playerName", "zakir");
$gameScore->set("cheatMode", false);
$gameScore->save();
echo 'New object created with objectId: ' . $gameScore->getObjectId();
} catch (Exception $ex) {
// Execute any logic that should take place if the save fails.
// error is a ParseException object with an error code and message.
echo 'Failed to create new object, with error message: ' . $ex->getMessage();
}
當在瀏覽器中跟隨url運行時,這也會給出連接超時: - http://:1337 / test
請幫助我,我該怎么做才能從我的客戶端sdk代碼訪問它?
如果您可以直接從服務器上的提示連接到服務器而不是從本地終端連接到服務器,則問題是某些防火牆問題(使用centos時可能是這種情況)或者解析過程沒有在正確的接口上偵聽。
請檢查綁定httpServer時使用的IP地址。 我認為它沒有綁定在正確的接口上。 要檢查哪些服務正在偵聽哪個接口,請嘗試以下操作:
netstat -an | grep LISTEN
如果您看到端口號為1337
localhost
或127.0.0.1
,則必須使用正確的IP來啟動服務器以進行綁定。 您可以將此作為附加參數傳遞,如下所示:
httpServer.listen(port, ip, function() { /* ... */ });
請注意,您可能還必須調整解析配置的serverURL
屬性以匹配正確的地址。 看到這里 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.