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:
-
/* 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? -
/* dagger */The logger does not acknowledge my requests, except for/favicon.ico. I find if I remove theapp.use(app.router);line, then I’ll see both/and/favicon.ico. What is going on here?
I checked in the express source code and the
app.setfunction simply assigns over top of any previous value it had. To get the behavior you’re looking for you’ll have to merge theview optionsobject in subsequent calls. This means you’ll probably have to jump through a couple of hoops. The connect package has a merge function that will work for this but to get it you’ll have to include it in yourpackage.json:You’ll need to get the
utilsobject from connect:app.set(option)with no value returns the current setting for the option so the second time you setview optionyou could do it this way:As for the problem you’re having with the logger, remember that app.use is adding pieces of middleware to a stack. As a request is being processed it calls each piece of middleware in the order they are originally configured and sometimes if a piece of middleware is able to fulfill its duty it will not pass control to subsequent middleware in the stack.
This is the case with the
routermiddleware when it fulfills a request for the'/'url whereloggeris not subsequently run. The reason the request forfavicon.icoshows up in the log stream is that none of the middleware was able to fulfill it (thestaticmiddleware would if you had apublic/favicon.icofile) and processing falls through to theloggermiddleware.To make your example work you’ll need to define the
loggermiddleware earlier in your stack, before theroutermiddleware.