Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

express res.locals() #29

Open
tcha-tcho opened this issue Dec 15, 2012 · 3 comments
Open

express res.locals() #29

tcha-tcho opened this issue Dec 15, 2012 · 3 comments

Comments

@tcha-tcho
Copy link

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! 👍

Cheers

@shimonchayim
Copy link
Member

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

@tcha-tcho
Copy link
Author

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
Copy link
Member

Thanks Edwardo! Looking into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants