我想将JSON数据从我的PHP脚本发送到node.js服务器。
我的PHP文件看起来:
$data = array("name" => "Lorerm", "age" => "18");
$data_string = json_encode($data);
$ch = curl_init('http://localhost:3000/push');
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))
);
curl_exec($ch);
我应该在node.js中做什么?
app.all('/push', function(req, res) {
// what to do here?
});
app.all('/push', function(req, res) {
console.log(req.body.name); // Lorem
console.log(req.body.name); // 18
res.status(200).json({data:req.body); // will give { name: 'Lorem',age:18'} in response
});
您的数据将在req.body 中提供
希望它能有所帮助:)