【问题标题】:Express Redis Session Crash with response status zero响应状态为零的 Express Redis 会话崩溃
【发布时间】:2017-12-12 19:10:45
【问题描述】:

我目前在使用 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 之外。我尝试搜索 google 和 stackOverflow,但我只能找到一些错误地在自己的代码中发送零状态的人,而不是 expressredis 会话的任何错误。

快速配置代码

下面是我用来连接 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"}

非常欢迎任何能指出我解决此崩溃或进一步调查的想法!

【问题讨论】:

  • 不设置sess.store时是否会发生这种情况(所以express-session会退回到使用内置的MemoryStore)?另外,您能否显示“正在等待mongoDB承诺响应查询”的代码
  • 好主意,@robertklep!至于 MongoDB 的代码,它可以是任何代码:我尝试插入任意 setTimeOut 并且根本不运行任何代码:它无论如何都崩溃了。最后它是一些依赖错误......我会发布一个答案来澄清。谢谢!

标签: angularjs node.js session express redis


【解决方案1】:

经过大量调试,我决定以最粗暴的方式更新我所有的 npm 包:

  • rm -rf node_modules 删除了我所有的依赖项
  • 用我所有依赖项的确切版本更新了我的package.json(没有“插入符号”或“大于”)
  • 然后npm install 在干净的项目上。

应用程序的会话再次开始工作! 希望这个问答可以帮助其他人通过这个奇怪的错误。

【讨论】:

    猜你喜欢
    • 2013-03-11
    • 1970-01-01
    • 2013-08-26
    • 2014-06-09
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 2023-03-20
    • 2020-12-04
    相关资源
    最近更新 更多