Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当在另一个文件中触发https请求时,如何将对象返回到客户端?_Javascript_Node.js - Fatal编程技术网

Javascript 当在另一个文件中触发https请求时,如何将对象返回到客户端?

Javascript 当在另一个文件中触发https请求时,如何将对象返回到客户端?,javascript,node.js,Javascript,Node.js,我试图让用户能够单击一个按钮,该按钮向我的节点服务器发送请求,然后在发送数据后,它启动另一个文件中的函数。我的问题不在于代码,我只是刚刚接触javascript,所以我不太确定应该如何将此对象返回给客户端,我很迷茫,我将在下面的代码中解释更多内容。我不确定我是否应该做像这样简单的事情 return obj; 或者,如果我应该使用async/await,我已经尝试过这样做,但不能完全理解它 http.createServer(function (req, res) { res.setH

我试图让用户能够单击一个按钮,该按钮向我的节点服务器发送请求,然后在发送数据后,它启动另一个文件中的函数。我的问题不在于代码,我只是刚刚接触javascript,所以我不太确定应该如何将此对象返回给客户端,我很迷茫,我将在下面的代码中解释更多内容。我不确定我是否应该做像这样简单的事情

return obj; 
或者,如果我应该使用async/await,我已经尝试过这样做,但不能完全理解它

http.createServer(function (req, res) {
    res.setHeader('Content-Type', 'application/json');
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Headers", "X-Requested-With")
    //res.writeHead(200, { 'Content-Type': 'text/plain' });
    var data = []
    if (req.method == "POST") {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        req.on('data', function (dat) {
            data.push(dat)
        })
        req.on('end', function () {
            var gamedata = Buffer.concat(data).toString();
            var game = JSON.parse(gamedata)

            games.getPing(game.placeId, 0)<--This is the function that gets fired in the other file


            res.end(/*some how get games.gameservers here to send back to client*/)

        })
    }


}).listen(9999)
http.createServer(函数(req,res){
res.setHeader('Content-Type','application/json');
res.setHeader(“访问控制允许原点”,“*”);
res.setHeader(“访问控制允许头”,“X请求的带”)
//res.writeHead(200,{'Content-Type':'text/plain'});
var数据=[]
如果(请求方法==“POST”){
res.writeHead(200,{'Content-Type':'text/plain'});
请求开启('数据',功能(dat){
数据推送(dat)
})
请求开启('end',函数(){
var gamedata=Buffer.concat(data.toString();
var game=JSON.parse(gamedata)
games.getPing(game.placeId,0)item.x<最低.x?item:lowest)
if(最低的.q!=最低的.p){
//console.log(最低)
游戏。游戏服务器。推送(最低)
}
games.getPing(id,index)
}捕获(e){
如果(e){
console.log(games.gameserver)
/*
这就是我的功能结束的地方,我需要能够将games.gameservers数组发送回cloud.js中的客户端
*/ 
}
}
}
})
}
})
})
r、 完()
}
}
在try/catch的错误中,是函数结束的地方。基本上,server.js中的res.end()需要包含games.gameservers对象才能返回到客户端


总体而言,我对javascript还比较陌生,所以我不确定是否应该使用return、async/await和回调。在Javascript中,如果使用不带“return”的函数,函数将返回未定义的。你不能用这个值做任何事。 在您的方法内部获取ping。使用return返回游戏。GameServer。 在http.createServer内部。这部分,你可以这样写

const result=[…games.getPing(game.placeId,0)](因为您有return,所以此函数将返回您的games.games服务器并将其分配给result)

res.end(/some-how-get-games.gameserver在此发送回客户端/) 希望这有帮助

那是行不通的:
games = {
gameservers: [],

getPing: function (id,index,callback) {
    var keepAliveAgent = new https.Agent({ keepAlive: true })

    var r = https.request(options, function (res) {
        var data = []
        res.on('data', function (d) {
            data.push(d)
        }).on('end', function () {
            var buf = Buffer.concat(data)
            var encodingheader = res.headers['content-encoding']
            if (encodingheader == 'gzip') {
                zlib.gunzip(buf, function (err, buffer) {
                    var o = JSON.parse(buffer.toString())
                    // o is what is returned
                    if (index < o.TotalCollectionSize) {
                        index = index + 10;
                        //console.log(index, o.TotalCollectionSize)
                        try {
                            let lowest = o.c.reduce((lowest, item) => item.x < lowest.x ? item : lowest)
                            if (lowest.q != lowest.p) {
                               // console.log(lowest)
                                games.gameservers.push(lowest)

                            }
                            games.getPing(id, index)
                        } catch (e) {
                            if (e) {
                                console.log(games.gameservers)
                               /*
                                This is where my function ends, I need to be able to send the games.gameservers array, back to the client in cloud.js

                               */ 
                            }
                        }
                    }
                })
            }
        })
    })
    r.end()
}
}