express--app.set


app.set(name,value)

name可以自己定義,也可以定義為應用設置(application settings) ,下面介紹幾個應用設置

 

Property Type Value Default

case sensitive routing

Boolean 支持區分大小寫 廢棄. Treats "/Foo" and "/foo" as the same.

env

String 設置環境模式

process.env.NODE_ENV(NODE_ENVenvironment variable) or “development”.

etag

Varied

設置ETag響應頭

 

jsonp callback name

String 指定默認的JSONP回調名稱

?callback=

json replacer

String JSON 替換的回調函數

null

json spaces

Number 在設置時,發送帶有指定數量空格的修飾的JSON字符串。 廢棄.

query parser

String

 

用於使用的查詢解析器,要么是“simple”,要么是“extended”。simple解析器基於節點的本地查詢解析器查詢字符串。extended 解析器基於qs

"extended"

strict routing

Boolean strict 路由模式 Disabled. Treats "/foo" and "/foo/" as the same by the router.

subdomain offset

Number The number of dot-separated parts of the host to remove to access subdomain. 2

trust proxy

Varied

 

表明該應用程序位於一個前端代理的后面,並使用X-Forwarded-*headers來確定客戶端的連接和IP地址。注意:X-Forwarded-*headers很容易被欺騙,檢測到的IP地址不可靠。

默認情況下,信任代理是禁用的。當啟用時,Express嘗試通過前端代理或一系列代理來確定客戶端的IP地址。要求的事情。然后,ips屬性包含了客戶端通過的IP地址的數組。要啟用它,請使用信任代理選項表中描述的值。

信任代理設置是使用proxy-addr包實現的。要了解更多信息,請參閱其文檔

Disabled.

views

String or Array 應用程序視圖的目錄或目錄數組。如果一個數組,視圖會按照數組中出現的順序查找 eg:app.set('views', path.join(__dirname, 'views'))

process.cwd() + '/views'

view cache

Boolean 支持視圖模板編譯緩存。

true in production.

view engine

String 當省略時使用的默認引擎擴展名   eg: app.set('view engine', 'ejs') //設置模板引擎為 ejs  

x-powered-by

Boolean Enables the "X-Powered-By: Express" HTTP header.

true

 

 

 

`trust proxy` 設置選項

 

Type Value
Boolean

如果正確,客戶端的IP地址被理解為X-Forwarded-*headers中最左邊的條目。

如果錯誤,程序是理解為直接面對互聯網和來自req.connection.remoteAddress客戶機的IP地址。這是默認設置。

IP addresses

一個IP地址,子網,或一個IP地址的數組,和子網來信任。下面是預配置的子網名稱列表

  • loopback - 127.0.0.1/8::1/128
  • linklocal - 169.254.0.0/16fe80::/10
  • uniquelocal - 10.0.0.0/8172.16.0.0/12192.168.0.0/16fc00::/7

Set IP addresses in any of the following ways:

app.set('trust proxy', 'loopback') // specify a single subnet app.set('trust proxy', 'loopback, 123.123.123.123') // specify a subnet and an address app.set('trust proxy', 'loopback, linklocal, uniquelocal') // specify multiple subnets as CSV app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']) // specify multiple subnets as an array

When specified, the IP addresses or the subnets are excluded from the address determination process, and the untrusted IP address nearest to the application server is determined as the client’s IP address.

Number

信任從前台代理服務器作為客戶機的第n個跳轉

Function

Custom trust implementation. Use this only if you know what you are doing.

app.set('trust proxy', function (ip) { if (ip === '127.0.0.1' || ip === '123.123.123.123') return true; // trusted IPs else return false; })

 

 

etag 設置選項
Type Value
Boolean

true enables weak ETag. This is the default setting.
false disables ETag altogether.

String If "strong", enables strong ETag.
If "weak", enables weak ETag.
Function

Custom ETag function implementation. Use this only if you know what you are doing.

app.set('etag', function (body, encoding) { return generateHash(body, encoding); // consider the function is defined })


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM