2016-11-22 55 views
1

我正在蘇格蘭盒我在Windows上運行的node.js(運行在Ubuntu的VM顛沛流離的LAMP堆棧)拒絕連接錯誤POST請求從PHP到Node.js的

這裏是我的Node.js代碼:

var express = require('express'); 
var app = express(); 
var http = require('http').Server(app); 
var io = require('socket.io')(http); 
var mysql = require('mysql'); 

http.listen(3000, function() { 
    console.log('listening on port *:3000'); 
}); 

app.post('/phpreq', function (req, res) { 
var content = req.body; 
console.log(content); 
res.end('ok'); 
}) 

這裏是我的PHP代碼:

$data = array("request" => "disconnect", "userid" => "5");                  
$data_string = json_encode($data); 

$ch = curl_init('http://localhost:3000/phpreq');                  
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                  
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                  
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
    'Content-Type: application/json',                     
    'Content-Length: ' . strlen($data_string))                  
);                             

echo curl_exec($ch)."\n"; 
if (curl_errno($ch)) { 
    die('Couldn\'t send request: ' . curl_error($ch)); 
} 
curl_close($ch); 

我收到的錯誤是:無法發送請求:無法連接到本地主機3000端口:連接被拒絕

我試過禁用我的防火牆並允許端口3000通過我的防火牆。

如何解決此錯誤?

+0

你可以嘗試使用,而不是本地主機127.0.0.1?可能是一個問題..我在使用mysql連接字符串中的本地主機時遇到過這種問題。 – Wizard

+0

同樣的錯誤:無法發送請求:無法連接到127.0.0.1端口3000:連接拒絕 –

+0

哦..我一直在分心:) ..你使用VM ..作爲回答下面說,你需要轉發你的本地端口到vm-port,或直接使用vm ip ..我想。 – Wizard

回答

0

我設法解決這個問題。我通過在node.js中使用0.0.0.0並使用192.168.33.10(cURL post請求中的vagrant的IP in PHP)來解決此問題。

我的新的Node.js代碼:

var express = require('express'); 
var app = express(); 
var http = require('http').Server(app); 
var io = require('socket.io')(http); 
var mysql = require('mysql'); 

http.listen(3000, function() { 
    console.log('listening on port *:3000'); 
}); 

app.post('/phpreq', function (req, res) { 
var content = req.body; 
console.log(content); 
res.end('ok'); 
}) 

新的PHP代碼:

$data = array("request" => "disconnect", "userid" => "5");                  
$data_string = json_encode($data); 

$ch = curl_init('http://192.168.33.1:3000/phpreq');                  
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                  
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                  
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
    'Content-Type: application/json',                     
    'Content-Length: ' . strlen($data_string))                  
);                             

echo curl_exec($ch)."\n"; 
if (curl_errno($ch)) { 
    die('Couldn\'t send request: ' . curl_error($ch)); 
} 
curl_close($ch); 
0

您必須確保您連接的端口3000位於正確的主機上。

您可能需要使用網關地址(在來賓操作系統上運行netstat -rn以查看它是什麼),使用本地IP地址而不是本地主機,以便使用正確的接口或使用某種形式的端口轉發(從客人3000到主人3000)。

當您在流浪盒中使用本地主機(或127.0.0.1,它是相同的),那麼您訪問該盒上的端口,而不是在主機上。

有幾種方式可以在Vagrant中配置網絡,因此您需要閱讀文檔並查看它是如何處理的。

請參見:

+0

感謝您的建議,我正在嘗試端口轉發,使其工作。我嘗試添加這一行:'config.vm.network:forwarded_port,guest:3000,host:3000',然後我使用vagrant重新加載。但它仍然給我同樣的錯誤:無法發送請求:無法連接到本地端口3000:連接被拒絕 –

相關問題