小编Apa*_*tus的帖子

NodeJS等待HTTP请求

我想编写一个从API请求令牌的应用程序。只要此令牌不可用,我就不想继续应用程序的其余部分。因此,它必须像同步HTTP请求一样。

我的目标是创建一个执行请求的函数,然后返回令牌,例如:

var token=getToken();  //After this function has finished
makeRequest(token);   //I want this function to be executed
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

javascript https asynchronous request node.js

3
推荐指数
2
解决办法
1万
查看次数

Node.JS https请求返回JSON

我有关于Node.js HTTPS请求的问题.请求转到服务器,该服务器将返回JSON响应.然后我想解析响应并将其存储在变量中并与其他函数一起使用.

let obj=JSON.parse(response);
return obj;
Run Code Online (Sandbox Code Playgroud)

我写的功能:

let protocol="https";
let hostStr="www.example.com";
let pathStr="***";

let students=makeRequest("ABCDEFG","getStudents"));
console.log(students);

function makeRequest(token,method){    
       let obj='';
        let options={
            host:hostStr,
            path:pathStr,
            method:"POST",
            headers:{"Cookie":"JSESSIONID="+token}
        };
        let https=require(protocol);
        callback = function(response){
            var str='';

            response.on('data',function(chunk){
                str+=chunk;
            });

            response.on('end',function(){
                obj=JSON.parse(str);
            });
        }
        let request=https.request(options,callback);
        request.write('{"id":"ID","method":"'+method+'","params":{},"jsonrpc":"2.0"}');
        request.end();
        return obj;
    }
Run Code Online (Sandbox Code Playgroud)

我希望你能帮帮我

javascript https json node.js

0
推荐指数
1
解决办法
3131
查看次数

标签 统计

https ×2

javascript ×2

node.js ×2

asynchronous ×1

json ×1

request ×1