Node / Express路由不等待Redis服务器回调

时间:2016-07-18 22:42:49

标签: javascript json node.js express redis

需要对下面的代码进行哪些具体更改,以便在Redis res.json(...)的任何打开调用返回后才会发送client.somecommand(..)命令?

下面的代码抛出了与client.hmset(uname, { ... }有关的错误,试图在调用res.json(...)后设置响应标头。当我将return res.json()命令移到client.exists(uname, function(err, reply) { ... }条件块结束后,而不是它们在块中的当前位置时,anonymous token值将被发送到客户端应用而不是生成的token值。这表示尚未返回Redis服务器的回调。

如何更改下面的代码,以便在返回Redis服务器回调之前,res.json( ... )命令无法运行?理想情况下,如果Redis服务器回调花费的时间过长,则会在发送错误消息之前等待一段时间。

通过将以下两行添加到文件顶部,将Redis添加到包含以下所有代码的routes.js文件中:

var redis = require('redis');
var client = redis.createClient();

以下是代码中对Redis服务器的各种调用:

client.exists(uname, function(err, reply) { ... }
client.hgetall(uname, function(err, object) { ... }
client.hmset(uname, { ... }
client.expire(uname, 10);

Node.js / Express.js API路由的完整代码是:

app.get('/user**', function(req, res) {
    console.log("You Hit The User Route TOP");
    request({
        method: 'GET',
        url: authServer + '/uaa/user', 
        json: true, 
        auth: {
            user: null,
            password: null,
            sendImmediately: true,
            bearer: bearerToken 
        }
    }, function (error, response, body) {
        if(error){
          console.log('ERROR with user request.');
          return res.sendStatus(500);  
        }
        else {
            var uname = '';var jwtUser = 'empty';var jwtJSON = { "token" : "anonymous" }
            console.log(response.statusCode);
            if(body['name']){ 
                uname = body['name'];console.log('uname is: ');console.log(uname);
                if(uname.length > 0) {
                    scopesLocal = body['oauth2Request'].scope.toString();
                    client.exists(uname, function(err, reply) {//Check to see if a Redis key for the user already exists
                        if (reply === 1) {//a redis key DOES exist
                            console.log('\"'+uname+'\" exists');
                            client.hgetall(uname, function(err, object) {//retrieve all the values in the hash/object that we just set
                            if(object) {
                                if(object["jwt"]) { 
                                    console.log('object[\"jwt\"] is: ');console.log(object["jwt"]); 
                                    jwtJSON = { "token" : object["jwt"] };
                                    console.log('jwtJSON is: ');console.log(jwtJSON);
                                    return res.json(jwtJSON);
                                }
                            }
                        });
                        } else {//a redis key DOES NOT exist
                            console.log('\"'+uname+'\" doesn\'t exist');
                            jwtUser = generateJwt(uname, authoritiesLocal);
                            client.hmset(uname, {//store a hash/object
                                'AccessToken': body['details'].tokenValue,
                                'TokenType': body['details'].tokenType,
                                'Authenticated': body['authenticated'],
                                'Principal': body['principal'],
                                'Scopes': scopesLocal.toString(), 
                                'Authorities' : authoritiesLocal,
                                'jwt' : jwtUser
                            });
                            jwtJSON = { "token" : jwtUser };console.log('jwtJSON is: ');console.log(jwtJSON);
                            return res.json(jwtJSON);
                        }
                        client.expire(uname, 10);//set the key to expire in 10 seconds.  use this to manage session length
                    });//end of Redis conditional block
                    console.log('jwtJSON is: ');console.log(jwtJSON);
                } else { console.log('uname is empty!'); }
                return res.json(jwtJSON);
            }
        };
    });
    console.log("You Hit The User Route BOTTOM");
});

nodemon终端中的错误消息是:

_http_outgoing.js:346
    throw new Error('Can\'t set headers after they are sent.');
    ^

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
    at ServerResponse.header (/home/user/nodejs_apps/oauth_seed_app/node_modules/express/lib/response.js:719:10)
    at ServerResponse.json (/home/user/nodejs_apps/oauth_seed_app/node_modules/express/lib/response.js:247:10)
    at Command.callback (/home/user/nodejs_apps/oauth_seed_app/app/routes.js:112:44)
    at normal_reply (/home/user/nodejs_apps/oauth_seed_app/node_modules/redis/index.js:714:21)
    at RedisClient.return_reply (/home/user/nodejs_apps/oauth_seed_app/node_modules/redis/index.js:816:9)
    at JavascriptRedisParser.Parser.returnReply (/home/user/nodejs_apps/oauth_seed_app/node_modules/redis/index.js:188:18)
    at JavascriptRedisParser.execute (/home/user/nodejs_apps/oauth_seed_app/node_modules/redis-parser/lib/parser.js:413:12)
    at Socket.<anonymous> (/home/user/nodejs_apps/oauth_seed_app/node_modules/redis/index.js:267:27)
    at emitOne (events.js:90:13)
    at Socket.emit (events.js:182:7)
    at readableAddChunk (_stream_readable.js:153:18)
    at Socket.Readable.push (_stream_readable.js:111:10)
    at TCP.onread (net.js:534:20)

我阅读this posting有关特定错误消息的信息。我读了this other posting关于如何等待回调的内容。我还阅读了this posting关于Redis对Node的回调。但我不知道如何将上述任何其他帖子的答案应用于上述代码中的Redis回调问题。

1 个答案:

答案 0 :(得分:0)

问题是OP中的return res.json(jwtJSON);命令没有被分隔成离散的if...else块。

解决方案是:

if(something) {
    //populate jwtJSON with a real JWT
    return res.json(jwtJSON);
} else {
    return res.json(jwtJSON);//this leaves the anonymous value
}  

对上面代码中的每个嵌套条件执行此分隔可以解决问题。