"不能POST /" |卷曲到nodejs(socket.io)

时间:2014-12-27 20:01:55

标签: php node.js curl

我在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脚本时,我收到一条回音消息“无法POST /”...我正在运行 socket.io侦听端口3000 ,但我不知道是否这是标准程序......任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

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

第2条)路线是/帖子

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

一样使用它
  curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:3000/posts');