如何在NodeJS中创建http请求并填充对象?

时间:2016-12-23 17:02:01

标签: node.js

我需要创建一个从服务器获取JSON的简单HTTP客户端。在我想填充一个对象之后。

我在Node.JS上使用请求模块,并尝试使用回调但没有成功。

可以请教我一个教程吗?任何帮助将不胜感激!

我的代码:

 var sessionInfo = {
     sessionId: 's1',
     loginName: ''
 };

function populateSessionInfo(body) {
     console.log("step4");
     sessionInfo.sessionId = "s2";
     sessionInfo.loginName = body.loginName;
     console.log("sessionInfo.sessionId =>" + sessionInfo.sessionId);
 };

function loginOnServer(callback) {
    console.log("step2");

    var options = {
        url: SERVER_URL + '/stwebapi/user/connect', //URL to hit
        method: 'POST',
        json: true, // request automatically parse body
        headers: {
            'User-Agent': USER_AGENT
        },
        form: {
            username: USERNAME,
            password: PASSWORD,
            loginMethod: 'byPassword',
            initialStatus: 1, // Available
            initialStatusMessage: 'I am Available @ SametimeBot!'
        }
    };

    request(options, function(error, response, body) {
        console.log("step3");
        if (error || response.statusCode != 200) {
            console.error('Failed to contact ST Proxy server');
            return callback(error);
        }
        if (body.returnCode == 500) {
            if (body.error == 4 || body.error == 2) {
                console.log('Invalid User or Password');
                return callback(error);
            }
        }
        return populateSessionInfo(body);
    });
    console.log("step5");
};


console.log("step1");
loginOnServer(sessionInfo);
console.log("step6");
console.log(sessionInfo);
console.log("step7");

当我跑步时,结果是:

step1
step2
step5
step6
{ sessionId: 's1', loginName: '' }
step7
step3
step4
sessionInfo.sessionId =>s2

1 个答案:

答案 0 :(得分:3)

您没有正确使用回调。

希望它对你有所帮助。

var sessionInfo = {
     sessionId: 's1',
     loginName: ''
 };

function populateSessionInfo(body) {
     console.log("step4");
     sessionInfo.sessionId = "s2";
     sessionInfo.loginName = body.loginName;
     console.log("sessionInfo.sessionId =>" + sessionInfo.sessionId);
 };

function loginOnServer(callback) {
    console.log("step2");

    var options = {
        url: SERVER_URL + '/stwebapi/user/connect', //URL to hit
        method: 'POST',
        json: true, // request automatically parse body
        headers: {
            'User-Agent': USER_AGENT
        },
        form: {
            username: USERNAME,
            password: PASSWORD,
            loginMethod: 'byPassword',
            initialStatus: 1, // Available
            initialStatusMessage: 'I am Available @ SametimeBot!'
        }
    };

    request(options, function(error, response, body) {
        console.log("step3");
        if (error || response.statusCode != 200) {
            console.error('Failed to contact ST Proxy server');
            return callback(error, null);
        }
        if (body.returnCode == 500) {
            if (body.error == 4 || body.error == 2) {
                console.log('Invalid User or Password');
                return callback(error, null);
            }
        }
        console.log("step5");
        return callback(body);
    });
};


console.log("step1");
loginOnServer(sessionInfo, function(err, body){
    populateSessionInfo(body);
    console.log("step6");
    console.log(sessionInfo);
    console.log("step7");
});