2017-12-04 10 views
0

私はサーバーに文字列のような "csv"を提出するはずの簡単なWebページを持っています。このためには、データ文字列を受け取って文字列をテキストファイルとして保存するAPIが必要です。PHPクライアントからノードjsサーバー。接続は決して「終了」しない

ページには、PHPを使用しており、サーバーは、ノードのjs

コード例です:

PHP:

$ch = curl_init(); 
$path = '/test.txt'; 
$url = "http://none.of.your.business.com:8280/"; 

$data = array('filePath' => $path, 
     'message' => $csv); 
$data_string = json_encode($string); 

curl_setopt($ch, CURLOPT_URL,$url); 
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)) 
); 

$result = curl_exec($ch); 
echo $result; 
curl_close($ch); 

ノードサーバー:このコードで

var net = require('net'); 

var HOST = '0.0.0.0'; 
var PORT = 8280; 


net.createServer(function(sock) { 

       var body = ""; 

       console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort); 

       sock.on('data', function (data) { 
           body += data; 
           console.log("Partial body: " + body); 
           }); 

       sock.on('end', function() { 

           console.log("ENDED"); 
           console.log("Body: " + body); 
           }); 

}).listen(PORT, HOST); 

console.log('Server listening on ' + HOST +':'+ PORT); 

(と他の選択肢)、「終了」イベントは決して呼び出されず、接続は決して閉じられない。 PHPスクリプトで300秒のタイムアウトが発生しました。私は何かをかなり単純だと思っています。私はPHPに "done"と言って何かを送るつもりですか?しかし、私はTCP接続に慣れていません。

[[email protected] fileWriter]# node index.js 
Server listening on 0.0.0.0:8280 
CONNECTED: xxx.xxx.xxx.xxx:52766 
Partial body: POST/HTTP/1.1 
Host: xxx.xxx.xxx.com:8280 
Accept: */* 
Content-Type: application/json 
Content-Length: 440 

{"filePath":"/test.txt","message":"\"WORKFLOWID\",\"PARENTWORKFLOWID\",\"SOURCEINSTANCEID\",\"WORKFLOWTYPECODE\",\"DATECREATED\",\"DATEUPDATED\",\"ASSETID\",\"PVN\",\"IVN\",\"PROVIDERID\",\"TITLE\",\"SERIESTITLE\",\"OFFERDATE\",\"LASTOFFERDATE\",\"REGION\",\"RELEASEPHASE\",\"WORKFLOWSTATUSID\",\"DURATIONMILLIS\"\\n\"\",\"\",\"\",\"\",\"\",\"04-Dec-17 14.55.05.000000 PM\",\"TITL0\",\"0\",\"0\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"-1\",\"\"\n"} 
+1

「接続:閉じる」をヘッダーに追加してください – YvesLeBorg

答えて

0

サーバー側(Node.js)の次のコードは私の問題を解決しました。応答の送信は接続を閉じたようです。

const http = require('http'); 
const fs = require('fs'); 
http.createServer((request, response) => { 
       const { headers, method, url } = request; 
       let body = []; 
       request.on('error', (err) => { 
           console.error(err); 
           }).on('data', (chunk) => { 
             body.push(chunk); 
             }).on('end',() => { 
               body = Buffer.concat(body).toString(); 

               var json = JSON.parse(body); 
               response.on('error', (err) => { 
                   console.error(err); 
                   }); 

               response.statusCode = 200; 
               response.setHeader('Content-Type', 'application/json'); 

               const responseBody = { headers, method, url, body }; 
               fs.appendFileSync(json.filePath,json.message); 
               response.write("Succesfully written " + json.filePath); 
             //  response.write(JSON.stringify(responseBody)); 
               response.end(); 

           }); 
}).listen(8280); 
関連する問題