【问题标题】:Express.js not responding when using sessionExpress.js 使用会话时没有响应
【发布时间】:2012-06-25 19:15:52
【问题描述】:

这是我的应用程序:

var express = require('express'),
    app = express.createServer(),
    RedisStore = require('connect-redis')(express);

app.configure (function(){
    app.use (express.logger({ format: ":method :url" }));
    app.use (express.bodyParser());
    app.use (express.cookieParser());
    app.use (express.session({ store: new RedisStore, secret: "totally random string" }));
});
app.configure ('development', function () {
    console.log ("Development mode.");
});
app.configure ('production', function () {
    console.log ("Production mode.");
});

app.get ('/', function (req, res) {
    res.contentType("text/html");
    res.send("hello", 200);
    res.end();
})

console.log ("Listening to *:"+8006);
app.listen (8006);

当我使用 localhost:8006/ 时它会挂在浏览器中

知道有什么问题吗?我可能遗漏了 1 个小细节。
它在 app.use (express.session... 行被注释掉时起作用

谢谢

【问题讨论】:

  • 您使用的是什么版本的快递? :)

标签: node.js session redis express


【解决方案1】:

删除res.end(),它会停止挂起并呈现页面。

var express = require('express'),
    app = express.createServer();

app.configure (function(){
    app.use (express.logger({ format: ":method :url" }));
    app.use (express.bodyParser());
    app.use (express.cookieParser());
});

app.configure ('development', function () {
    console.log ("Development mode.");
});
app.configure ('production', function () {
    console.log ("Production mode.");
});

app.get ('/', function (req, res) {
    res.contentType("text/html");
    res.send("hello");
})

console.log ("Listening to *:"+8006);
app.listen (8006);

【讨论】:

  • 澄清一下,原因是res.send是Express添加到node的http.ServerResponse类的方法,而res.end是native方法。除其他外,res.send 在完成后调用res.end,然后自己调用res.end 会让您陷入麻烦,因为它并非设计为被调用两次(实际上,响应对象可能不再有效那个时候)。
  • 谢谢。像没有快递一样“结束”连接是有道理的。为我节省了几个小时,谢谢
猜你喜欢
  • 2012-08-20
  • 1970-01-01
  • 2012-12-22
  • 1970-01-01
  • 2014-03-09
  • 1970-01-01
  • 2013-02-12
  • 2014-05-15
  • 2019-01-19
相关资源
最近更新 更多