Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add to 'view options' in Express?

I am learning to use Express. I want to do:

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.set('view options', { layout: false });    /* asterisk */
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);                           /* dagger */
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  app.use(express.logger('dev'));
  app.set('view options', { pretty: true });     /* asterisk */
});

The additions I made were:

  • use 'layout:false' for Jade.
  • pretty-print the HTML in Jade.
  • turn on the logger, with the 'dev' format

There are two problems:

  1. /* asterisk */ when I set 'pretty: true' I am overriding my previous options, rather than adding to them. I.e., my program breaks unless I add { pretty: true, layout: false } which feels redundant and can't be correct. How can I correct it so that I am only "modifying" the view options, rather than "defining" them?

  2. /* dagger */ The logger does not acknowledge my requests, except for /favicon.ico. I find if I remove the app.use(app.router); line, then I'll see both / and /favicon.ico. What is going on here?

like image 847
Robert Martin Avatar asked Oct 28 '25 17:10

Robert Martin


1 Answers

As of Express 3.x the use of app.set('view options') is no longer the correct way. https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x These options are now set in your app.js using app.locals as in

app.locals.pretty = true;

or to set custom delimiters for ejs

app.locals.open = '}}';
app.locals.close = '{{';
like image 94
Bozeman Tofu Shop Avatar answered Oct 31 '25 07:10

Bozeman Tofu Shop