“无法POST /” | curl到nodejs(socket.io) [英] "Cannot POST /" | curl to nodejs (socket.io)
本文介绍了“无法POST /” | curl到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:')); $ b $ c 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服务器运行以下代码:
$ b
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);
//服务器尝试向我们发送活动消息
var form = new formidable.IncomingForm();
form.parse ,函数(err,fields,files){
res.writeHead(200,[[Content-Type,text / plain]
,[Content-Length 0]
]);
res.write('');
res.end();
//sys.puts(sys.inspect field:fields},true,4));
handleServerNotice(fields);
});
}
}
} );不幸的是,当我运行php脚本时,我收到一条回复消息Can not POST /...。我正在运行 socket.io在端口3000侦听,但我不知道这是否是标准过程...任何人都可以帮助我在这一个?解决方案第1)您的API不接受POST请求,因为app.get,您的API是在GET请求的工作,并且您从POST调用它。
第二)路径是/ posts
curl_setopt($ ch,CURLOPT_URL, 'http://127.0.0.1');
使用
curl_setopt($ ch,CURLOPT_URL,'http://127.0.0.1:3000/posts');
I got the following code from another question here in 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);
with my nodejs server running the following code:
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);
});
}
}
});
Unfortunately, when I run the php script I get an echo message "Cannot POST /"... I'm running socket.io listening in port 3000, but I don't know if this is the standard procedure... Can anyone help me in this one?
解决方案 1st ) Your API is not accepting POST request because of app.get, You API is work on GET requiest and you are calling it from POST.
2nd) The route is /posts
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1');
use it like
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:3000/posts');
这篇关于“无法POST /” | curl到nodejs(socket.io)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文