如何在Express中添加“查看选项”?(How do I add to 'view options' in Express?)

编程入门 行业动态 更新时间:2024-10-19 04:30:49
如何在Express中添加“查看选项”?(How do I add to 'view options' in Express?)

我正在学习使用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'格式打开记录器

有两个问题:

/* asterisk */当我设置'漂亮:真'我覆盖了我以前的选项,而不是添加到它们。 也就是说,我的程序会中断,除非我添加{ pretty: true, layout: false } ,这会感觉多余,并且不能正确。 我如何纠正它,以便我只是“修改”视图选项,而不是“定义”它们?

/* dagger */除了/favicon.ico之外,记录器不会确认我的请求。 我发现如果我删除app.use(app.router); 行,那么我会看到/和/favicon.ico 。 这里发生了什么?

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 the app.use(app.router); line, then I'll see both / and /favicon.ico. What is going on here?

最满意答案

我检查了快速源代码, app.set函数只是分配了它以前的任何值的顶部。 为了获得你正在寻找的行为,你必须在随后的调用中合并view options对象。 这意味着你可能不得不跳过几圈。 连接包有一个合并功能,将为此工作,但要得到它,你必须将它包含在你的package.json :

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

您需要从连接中获取utils对象:

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

没有值的app.set(option)返回app.set(option)的当前设置,所以第二次设置view option ,可以这样做:

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

至于你使用记录器时遇到的问题,请记住app.use正在将一些中间件添加到堆栈中。 在处理请求时,它会按照它们最初配置的顺序调用每个中间件,有时如果一个中间件能够履行其职责,它将不会将控制权交给堆栈中的后续中间件。

当router中间件满足请求logger未被运行的'/' url时,就是这种情况。 favicon.ico的请求显示在日志流中的原因是,没有一个中间件能够满足它( static中间件会在public/favicon.ico文件中出现),并且处理流向logger中间件。

为了使您的示例工作,您需要先在堆栈中定义logger中间件,然后再安装router中间件。

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.

更多推荐

本文发布于:2023-08-01 15:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项   如何在   Express   options   add

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!