如何将res从一个函数传递给另一个?

FB.api('4', function (res) { if(!res || res.error) { console.log(!res ? 'error occurred' : res.error); return; } console.log(res.id); console.log(res.name); }); // viewed at http://localhost:8080 app.get('/test', function(req, res) { res.sendFile(path.join(__dirname + '/index.html')); }); 

我创build了这个返回一些数据的小FB.api调用。 现在我想在访问localhost:8080 / test时显示这个数据,我该怎么做? 有什么办法做这样的事情? 有人能指点我一些可能的文件吗?

首先,像这样在快速路线中添加FB.api呼叫

 app.get('/something', function(req, res) { FB.api('4', function (result) { if(!res || res.error) { return res.send(500, 'error'); } res.send(result); }); }); 

然后在index.html里面,你可以添加一些javascript到你的页面,并创build一个XHR来调用“localhost:8080 / something”

的index.html

 <script type="text/javascript"> var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://localhost:8080/something'); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { var data = xhr.responseText; //Do whatever you want with this data. } } xhr.send(); </script>