ql-io / cluster2

A node.js (>= 0.8.x) compatible multi-process management module
http://ql-io.github.com/cluster2/
Other
280 stars 35 forks source link

express res.locals() #29

Open tcha-tcho opened 11 years ago

tcha-tcho commented 11 years ago

hi!

i just need to report a strange behaviour that I'm having.

var
    Cluster = require('cluster2'),
    express = require('express'),
    engine = require('ejs-locals'),
    ejs = require('ejs'),
    app = express(),
    port = 8080;

var serving = true;
GLOBAL.global_test = "global testing"

app.configure(function() {
  app.engine('ejs', engine);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.set('view options', {
    layout: false
  });
});

app.get('/', function(req, res) {
    res.locals({test:"testing response"})

    //this is wrong, res.locals have to propagate down the chain
    res.render("./test.ejs",res.locals()); 

    if(!serving)  {
        req.connection.end();
    }
});

var c = new Cluster({
    port: port,
    cluster: true,
    timeout: 500,
    ecv: {
        path: '/ecv',
        control: true,
        monitor: '/',
        validator: function() {
            return true;
        }
    }
});

c.on('died', function(pid) {
    console.log('Worker ' + pid + ' died');
});
c.on('forked', function(pid) {
    console.log('Worker ' + pid + ' forked');
});

c.listen(function(cb) {
    cb(app);
});

res.locals() is not working as should be. Using cluster2 I have to pass the object as argument on rendering. But in expressjs this will automatically be passed into res object and bubble until reach the engine system.

<html>
<head>
  <title></title>
</head>
<body>
  Testing:<br>
  <%= test %><br>
  <%= global_test %>
</body>
</html>

My first guess that we have a problem with globals. But globals is working fine. Any guess?

Thank you, cluster2 is a great code! :+1:

Cheers

shimonchayim commented 11 years ago

Thanks for this detailed example. Can you also provide a non-cluster example on how this should work?

tcha-tcho commented 11 years ago

Hi shimonchayim!

The only thing that should work differently is this:

res.render("./test.ejs",res.locals());

this should work like this:

res.render("./test.ejs");

Here the documentation. Nothing so drastic, but strange :)

Thank you!

shimonchayim commented 11 years ago

Thanks Edwardo! Looking into it.