繁体   English   中英

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

1)由于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');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM