What app.set function does (express.js)?

前端 未结 3 1513
攒了一身酷
攒了一身酷 2021-02-13 14:09

I am new to node.js and was going through an example could not understand app.set(\'title\', \'My Site\'); does please help?

相关标签:
3条回答
  • 2021-02-13 14:26

    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
    
    0 讨论(0)
  • 2021-02-13 14:40

    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.

    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.

    etag -> Varied -> Set the ETag response header. For possible values, see the etag options table.

    jsonp callback name -> String -> Specifies the default JSONP callback name.

    json replacer -> Varied -> The 'replacer' argument used by JSON.stringify. NOTE: Sub-apps will inherit the value of this setting.

    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.

    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.

    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.

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

    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.

    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.

    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.

    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'

    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.

    0 讨论(0)
  • 2021-02-13 14:41

    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

    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.

    0 讨论(0)
提交回复
热议问题