4

Here is my app:

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);

It hangs in the browser when I use localhost:8006/

Any idea whats wrong? I am probably missing 1 little detail.
It works when the app.use (express.session... line is commented out

Thanks

1
  • What version of express are you using? :) Commented Jun 24, 2012 at 0:44

1 Answer 1

3

Remove the res.end() and it stops the hanging and renders the page.

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Just to clarify a little, the reason for this is that res.send is a method added to node's http.ServerResponse class by Express, whereas res.end is a native method. Among other things, res.send invokes res.end when it's done, and invoking res.end yourself afterwards gets you into trouble, since it wasn't designed to be invoked twice (in fact, the response object is probably no longer valid by that time).
Thank you. It made sense to 'end' the connection like doing so without express.. Saved hours for me, thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.