4

Expressの使い方を学んでいます。私はやってみたいです:

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 */
});

私が行った追加は次のとおりです。

  • Jade には「layout:false」を使用します。
  • Jade で HTML をきれいに印刷します。
  • 「dev」形式でロガーをオンにします

2 つの問題があります。

  1. /* asterisk */「pretty: true」を設定すると、以前のオプションに追加するのではなく、上書きします。{ pretty: true, layout: false }つまり、冗長だと感じて正しくないものを追加しない限り、私のプログラムは壊れます。ビュー オプションを「定義」するのではなく、単に「変更」するように修正するにはどうすればよいですか?

  2. /* dagger */を除いて、ロガーは私の要求を認識しません/favicon.ico。行を削除すると、とのapp.use(app.router);両方が表示されます。ここで何が起こっているのですか?//favicon.ico

4

2 に答える 2

2

I checked in the express source code and the app.set function simply assigns over top of any previous value it had. To get the behavior you're looking for you'll have to merge the view options object 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 your package.json:

"dependencies": {
  "express": "2.5.5"
, "jade": ">= 0.0.1"
, "connect": "1.X"
}

You'll need to get the utils object from connect:

var utils = require('connect').utils;

app.set(option) with no value returns the current setting for the option so the second time you set view option you could do it this way:

app.set('view options', utils.merge(app.set('view options'), { pretty: true }));

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 router middleware when it fulfills a request for the '/' url where logger is not subsequently run. The reason the request for favicon.ico shows up in the log stream is that none of the middleware was able to fulfill it (the static middleware would if you had a public/favicon.ico file) and processing falls through to the logger middleware.

To make your example work you'll need to define the logger middleware earlier in your stack, before the router middleware.

于 2012-04-23T06:48:09.007 に答える