9

I am trying to port a script I wrote for groovy over to jade, and have run into a stumbling block

I need to access the user-agent from inside a jade file. Here is what I have tried so far:

 - var agent = req.headers['user-agent'];
 - var agent = headers['user-agent'];
 - var agent = navigator.userAgent;

every time I get a 500 error from express. Is this even possible?

I know I could do it in a module and pass it to the render statement, but that would mean passing it to EVERY render, as it needs to be global.

Very new to node, and confused. Thanks SO.

Fresheyeball
  • 29,567
  • 20
  • 102
  • 164

2 Answers2

15

Just write your own tiny middleware

app.use(function(req, res, next) {
  res.locals.ua = req.get('User-Agent');
  next();
});

Put this before your app.router

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());

  // here
  app.use(function(req, res, next) {
    res.locals.ua = req.get('User-Agent');
    next();
  });

  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

Then you can use the ua variable in any jade template (for example index.jade)

extends layout

block content
  h1= title
  p Welcome to #{title}
  p=ua
zemirco
  • 16,171
  • 8
  • 62
  • 96
  • Worked great. Just one quick follow up question, why is the placement under app.configure important? – Fresheyeball Mar 13 '13 at 21:47
  • 1
    good question! you could probably put it anywhere you want. I tend to put my middleware just before the router because then all the processing (parsing the body, sessions, etc.) is done and the routes are handled. – zemirco Mar 13 '13 at 21:57
  • I put it at the bottom and it broke, por que? – Fresheyeball Mar 13 '13 at 22:52
  • 1
    The router already took over and and rendered the views. Therefore your custom `res.locals.ua` variable wasn't set yet. By `anywhere` I ment anywhere before the router :) – zemirco Mar 14 '13 at 06:18
  • Good tip, worked for me. I used `req.getHeader('User-Agent')` and it worked for me in `express 3.4.0` – Alan David Garcia Mar 25 '14 at 02:53
4

You can pass user-agent from express to jade : (see here)

app.get('/index', function(req, res){
    res.render('home.jade', {
    locals: {
            useragent: req.getHeader('User-Agent')
            }
    });
    res.end();
});

in your jade file

html
  body
    h1 #{useragent}
script(type='text/javascript')
  var useragent = #{useragent};
user568109
  • 47,225
  • 17
  • 99
  • 123
  • Is this the only way? There is no way to do so globally? – Fresheyeball Mar 13 '13 at 05:50
  • See here : http://stackoverflow.com/questions/12088557/global-variable-for-jade-templates-in-node-js jade scope is different from node, so you must pass it in render if you want to use it. – user568109 Mar 13 '13 at 05:59