Express Redis Session Crash,响应状态为零

时间:2017-07-08 16:46:01

标签: angularjs node.js session express redis

我目前在使用Redis作为NodeJS-Express应用程序的会话存储时遇到了一些问题。在某些时候,当我的应用程序正在等待mongoDB承诺响应查询时,Redis会话突然崩溃了整个应用程序。

堆栈跟踪:

_http_server.js:192
    throw new RangeError(`Invalid status code: ${statusCode}`);
    ^

RangeError: Invalid status code: 0
    at ServerResponse.writeHead (_http_server.js:192:11)
    at ServerResponse.writeHead (/my_project_path/node_modules/on-headers/index.js:55:19)
    at ServerResponse.writeHead (/my_project_path/node_modules/on-headers/index.js:55:19)
    at ServerResponse.writeHead (/my_project_path/node_modules/on-headers/index.js:55:19)
    at ServerResponse._implicitHeader (_http_server.js:157:8)
    at ServerResponse.end (/my_project_path/node_modules/compression/index.js:102:14)
    at writeend (/my_project_path/node_modules/express-session/index.js:261:22)
    at Command.ontouch (/my_project_path/node_modules/express-session/index.js:348:11)
    at Command.callback (/my_project_path/node_modules/connect-redis/lib/connect-redis.js:248:10)
    at normal_reply (/my_project_path/node_modules/redis/index.js:721:21)
    at RedisClient.return_reply (/my_project_path/node_modules/redis/index.js:819:9)
    at JavascriptRedisParser.returnReply (/my_project_path/node_modules/redis/index.js:192:18)
    at JavascriptRedisParser.execute (/my_project_path/node_modules/redis-parser/lib/parser.js:574:12)
    at Socket.<anonymous> (/my_project_path/node_modules/redis/index.js:274:27)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:547:20)

我已经尝试了很多方法来拦截错误,但无济于事:它发生在任何钩子或 console.log 我尝试。我尝试搜索谷歌和stackOverflow,但我能找到的是一些错误地在他们自己的代码中发送零状态的人,而不是任何明确&lt; - &gt; redis会话的错误。

快速配置代码

下面是我用来连接Express与Redis的代码:

const config = {...config data here...}
const session = require('express-session'); // version 1.15.3
const redis = require('redis'); // version 2.7.1, 
const RedisStore = require('connect-redis')(session); // version 3.3.0

const redisClient = redis.createClient({
  host: config.redisCache.host,
  port: config.redisCache.port,
  auth_pass: config.redisCache.token
});
redisClient.on("error", function (err) {
  // This is never called
  console.error("Redis Error " + JSON.stringify(err,null,2));
});

const sess = {
    resave: false, // don't save session if unmodified
    saveUninitialized: true,// don't create session until something stored,
    secret: config.secrets.session,
    store: new RedisStore({client:redisClient}),
    logErrors: function(_err) {
      // This is never called either
      console.error('Session error:' + JSON.stringify(_err,null,2)) 
    }
  };
app.use(session(sess));

请求数据

下面是我要发送的两个请求。一,来自我的客户端AngularJS应用程序,崩溃了服务器。另一个收到回复就好了,以完成我的困惑。

// AngularJS request - crashes server with above stacktrace
{
  "host": "localhost:8082",
  "connection": "keep-alive",
  "content-length": "54",
  "origin": "http://localhost:8082",
  "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
  "content-type": "application/json;charset=UTF-8",
  "accept": "application/json, text/plain, */*",
  "apptoken": "my_project_app_token",
  "referer": "http://localhost:8082/",
  "accept-encoding": "gzip, deflate, br",
  "accept-language": "en-US,en;q=0.8,pt;q=0.6",
  "cookie": "connect.sid=s%3A14363a00-6285-11e7-8c76-9366761bb151.1Yg2b6gsLR8uJ7OCwLshZDacMbNC3XASI4%2BxVlRZ%2BXg"
}

// PostMan request, works just fine! (??)
{
  "host": "localhost:8082",
  "connection": "keep-alive",
  "content-length": "57",
  "postman-token": "86a514cd-7c19-85ee-d831-3b032849d430",
  "cache-control": "no-cache",
  "origin": "chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop",
  "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
  "apptoken": "my_project_app_token",
  "content-type": "application/json;charset=UTF-8",
  "accept": "*/*",
  "accept-encoding": "gzip, deflate, br",
  "accept-language": "en-US,en;q=0.8,pt;q=0.6",
  "cookie": "connect.sid=s%3AdUYMmy7KkcUF-tS9eHHbOTgNW4YrKx92.elK9FAP6kuZ3aazh8Z5X8m1lSZX%2BO4sIcex0LLD7JBQ"
}

// For completeness' sake, here is the POST request body
{"phone":"9999999999999","email":"xxxxxx@xxxx.xxx.xx"}

任何能够帮我解决此问题或进一步调查的想法都会受到欢迎!

1 个答案:

答案 0 :(得分:0)

经过大量调试后,我决定以最粗野的方式更新所有npm个软件包:

  • 使用rm -rf node_modules
  • 删除了所有依赖项
  • 使用我所有依赖项的确切版本更新了我的package.json(没有&#34;插入符&#34;或&#34;大于&#34;)
  • 然后在清洁项目上npm install

该应用的会话再次开始运作! 希望这个Q&amp; A帮助其他人通过这个奇怪的错误。