javascript app.set 函数有什么作用(express.js)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25229129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What app.set function does (express.js)?
提问by Paras
I am new to node.js and was going through an example could not understand app.set('title', 'My Site'); does please help?
我是 node.js 的新手,正在经历一个无法理解 app.set('title', 'My Site'); 的例子。请问有帮助吗?
回答by ffflabs
You can use the express instance to store and retrieve variables. In this case, you can set the title to be "My Site" and retrieve it later with something like
您可以使用 express 实例来存储和检索变量。在这种情况下,您可以将标题设置为“我的网站”,然后使用类似的内容检索它
var title = app.get('title');
without the need to declare and keep a global variable messing around.
无需声明和保持全局变量乱七八糟。
The name of the parameter means nothing. You could do
参数的名称没有任何意义。你可以做
app.set('jabberwocky', 'correct battery horse staples');
as well. If you're using express with jade, for example, you might need to retrieve the value of 'jabberwocky' in a template, further along the line.
以及。例如,如果您将 express 与 jade 一起使用,您可能需要在模板中检索 'jabberwocky' 的值,更进一步。
回答by Vlad Pana
I know I am a bit late to the party but I had the same problem and that's how I got here. After I did some research, I fell that the answer is a bit incomplete. In case that first parameter is an application setting, the following table must be read.
我知道我参加聚会有点晚了,但我遇到了同样的问题,这就是我来到这里的方式。在我做了一些研究之后,我觉得答案有点不完整。如果第一个参数是应用程序设置,则必须阅读下表。
Property -> Type -> Description
属性 -> 类型 -> 描述
case sensitive routing -> Boolean -> Enable case sensitivity. When enabled, "/Foo" and "/foo" are different routes. When disabled, "/Foo" and "/foo" are treated the same.
区分大小写的路由 -> 布尔值 -> 启用区分大小写。启用后,“/Foo”和“/foo”是不同的路由。禁用时,“/Foo”和“/foo”的处理方式相同。
env ->
String ->
Environment mode. Be sure to set to "production" in a production environment; see Production best practices: performance and reliability.
process.env.NODE_ENV (NODE_ENV environment variable) or “development” if NODE_ENV is not set.
env -> 字符串 -> 环境模式。一定要在生产环境中设置为“production”;请参阅生产最佳实践:性能和可靠性。
process.env.NODE_ENV(NODE_ENV 环境变量)或“development”(如果未设置 NODE_ENV)。
etag -> Varied -> Set the ETag response header. For possible values, see the etag options table.
etag -> Varied -> 设置 ETag 响应头。有关可能的值,请参阅 etag 选项表。
jsonp callback name -> String -> Specifies the default JSONP callback name.
jsonp 回调名称 -> String -> 指定默认的 JSONP 回调名称。
json replacer ->
Varied ->
The 'replacer' argument used by JSON.stringify
.
NOTE: Sub-apps will inherit the value of this setting.
json 替换器 -> 各种 -> 使用的“替换器”参数JSON.stringify
。注意:子应用程序将继承此设置的值。
json spaces ->
Varied ->
The 'space' argument used by JSON.stringify
. This is typically set to the number of spaces to use to indent prettified JSON.
NOTE: Sub-apps will inherit the value of this setting.
json 空间 -> 变化 -> 使用的“空间”参数JSON.stringify
。这通常设置为用于缩进美化 JSON 的空格数。注意:子应用程序将继承此设置的值。
query parser -> Varied -> Disable query parsing by setting the value to false, or set the query parser to use either “simple” or “extended” or a custom query string parsing function. The simple query parser is based on Node's native query parser, querystring. The extended query parser is based on qs. A custom query string parsing function will receive the complete query string, and must return an object of query keys and their values.
查询解析器 -> 可变 -> 通过将值设置为 false 来禁用查询解析,或者将查询解析器设置为使用“简单”或“扩展”或自定义查询字符串解析函数。简单查询解析器基于 Node 的原生查询解析器 querystring。扩展查询解析器基于 qs。自定义查询字符串解析函数将接收完整的查询字符串,并且必须返回查询键及其值的对象。
strict routing -> Boolean -> Enable strict routing. When enabled, the router treats "/foo" and "/foo/" as different. Otherwise, the router treats "/foo" and "/foo/" as the same. NOTE: Sub-apps will inherit the value of this setting.
严格路由 -> 布尔值 -> 启用严格路由。启用后,路由器将“/foo”和“/foo/”视为不同。否则,路由器将“/foo”和“/foo/”视为相同。注意:子应用程序将继承此设置的值。
subdomain offset -> Number -> The number of dot-separated parts of the host to remove to access subdomain.
subdomain offset -> Number -> 要访问子域的主机以点分隔的部分的数量。
trust proxy -> Varied -> Indicates the app is behind a front-facing proxy, and to use the X-Forwarded-* headers to determine the connection and the IP address of the client. NOTE: X-Forwarded-* headers are easily spoofed and the detected IP addresses are unreliable.
trust proxy -> Varied -> 表示应用程序位于前置代理之后,并使用 X-Forwarded-* 标头来确定客户端的连接和 IP 地址。注意:X-Forwarded-* 标头很容易被欺骗并且检测到的 IP 地址不可靠。
When enabled, Express attempts to determine the IP address of the client connected through the front-facing proxy, or series of proxies. The req.ips
property, then contains an array of IP addresses the client is connected through. To enable it, use the values described in the trust proxy options table.
启用后,Express 会尝试确定通过前置代理或一系列代理连接的客户端的 IP 地址。然后req.ips
,该属性包含客户端连接的 IP 地址数组。要启用它,请使用信任代理选项表中描述的值。
The trust proxy
setting is implemented using the proxy-addr package. For more information, see its documentation.
NOTE: Sub-apps will inherit the value of this setting, even though it has a default value.
该trust proxy
设置是使用 proxy-addr 包实现的。有关更多信息,请参阅其文档。注意:子应用程序将继承此设置的值,即使它具有默认值。
views ->
String or Array ->
A directory or an array of directories for the application's views. If an array, the views are looked up in the order they occur in the array.
process.cwd() + '/views'
视图 -> 字符串或数组 -> 应用程序视图的目录或目录数组。如果是数组,则按照它们在数组中出现的顺序查找视图。
process.cwd() + '/views'
view cache -> Boolean -> Enables view template compilation caching.
视图缓存 -> 布尔值 -> 启用视图模板编译缓存。
view engine -> String -> The default engine extension to use when omitted. NOTE: Sub-apps will inherit the value of this setting.
查看引擎 -> 字符串 -> 省略时使用的默认引擎扩展。注意:子应用程序将继承此设置的值。
x-powered-by -> Boolean -> Enables the "X-Powered-By: Express" HTTP header.
x-powered-by -> Boolean -> 启用“X-Powered-By: Express”HTTP 标头。
回答by ROBIN CHACKO
Use the following
使用以下
app.set('views', path.join(__dirname, 'views'));
This will set your apps view folder to something like:
这会将您的应用程序视图文件夹设置为:
/Users/jilles/Project/myApp/views