Can';t从nodejs向PHP脚本发送post并接收echo

Can';t从nodejs向PHP脚本发送post并接收echo,php,node.js,Php,Node.js,我尝试过使用变量data等于'test=yes'或data等于{test:'yes'} 下面是php脚本 if(isset($_POST['test'])){ error_log("inside"); echo "and I'd also like to return this"; }; 下面是一段代码: function postToPHP(data, path){ var httpreq = require('http'); var querystring

我尝试过使用变量
data
等于
'test=yes'
data
等于
{test:'yes'}

下面是php脚本

if(isset($_POST['test'])){ 
   error_log("inside");
   echo "and I'd also like to return this";
};
下面是一段代码:

function postToPHP(data, path){

    var httpreq = require('http');
    var querystring = require("querystring");
    var data = querystring.stringify(data);

    var options = {
        host : 'localhost',
        path : 'www' + path, //path is well defined in the actual code
        method : 'POST',
        headers : {
            'Content-Type' : 'application/x-www-form-urlencoded',
            'Content-Length' : data.length
        }
    };

    var buffer = "";

    var reqPost = httpreq.request(options, function(res) {

        res.on('data', function(d) {
            buffer = buffer+data;
        });
        res.on('end', function() {
           return buffer;
        });
    });

    reqPost.write(data);
    reqPost.end();

}
以及postToPhp的号召

       //treat message?
        var message = "test=yes";
        //OR
        var message = "{test:'yes'}";

        var buffer = postToPHP(message,"path");
        console.log("buffer from PHP",buffer);
缓冲区
未定义


错误日志中没有显示任何内容,我假设我无法理解的代码不起作用,因此我希望有人能帮助我解决这个问题。

备注:
res.on('end',function(){return buffer;})-你认为你回到这里是为了什么?实际上,该返回值当前将丢失。正在执行的
postToPHP()
在哪里?@ChenAsraf请检查编辑,ty@Sirko请检查编辑,ty!不能像这样从回调函数内部返回值。要么将回调传递给
posttohp()
,在收到数据时执行回调,要么从中创建承诺。