“无法开机自检 /” |curl to nodejs (socket.io).


"Cannot POST /" | curl to nodejs (socket.io)

我从SO的另一个问题中得到了以下代码。

function notifyNode($type, $project_id, $from_user, $data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch, CURLOPT_PORT, 3000);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, CURLOPT_POST, true);
    $pf = array('f' => $type, 'pid' => $project_id, 'user_from' => $from_user, 
         'data' => array());
    foreach($data as $k => $v) {
        $pf['data'][$k] = $v;
    }
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($pf));
    curl_exec($ch);
    curl_close($ch);
}
$test = array();
array_push($test,"first","second","third");
notifyNode("test","moretest","user2",$test);

使用我的 NodeJS 服务器运行以下代码:

app.get('/posts', function(req, res){
  if(res.socket.remoteAddress == '127.0.0.1') {
   console.log("connection received");
    if(req.method == 'POST') {
        console.log("POST received");
        // The server is trying to send us an activity message
        var form = new formidable.IncomingForm();
        form.parse(req, function(err, fields, files) {
            res.writeHead(200, [[ "Content-Type", "text/plain"]
                    , ["Content-Length", 0]
                    ]);
            res.write('');
            res.end();
            //sys.puts(sys.inspect({fields: fields}, true, 4));
            handleServerNotice(fields);                
        });
    }
}
});

不幸的是,当我运行 php 脚本时,我收到一条回显消息"无法开机自检/"......我正在运行 socket.io 侦听端口 3000,但我不知道这是否是标准过程......谁能帮我解决这个问题?

1st ) 由于 app.get 的原因,您的 API 不接受 POST 请求,您的 API 正在处理 GET requiest,并且您正在从 POST 调用它。

2nd)路线是/帖子

  curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1');

像这样使用它

  curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:3000/posts');