Novice's express4.0 Note API

Source: Internet
Author: User

App.set (name, value) settings

The following settings would alter how Express behaves:

Express built-in parameters

    • env Environment mode, defaults to process.env.NODE_ENV (node_env environment variable) or "development"
    • trust proxy Enables reverse proxy support, disabled by default
    • subdomain offsetThe number of dot-separated parts of the host to remove to access subdomain, both by default
    • jsonp callback name Changes the default callback name of?callback=
    • json replacer JSON Replacer callback, NULL by default
    • case sensitive routing Enable case sensitivity, disabled by default, treating "/foo" and "/foo" as the same
    • strict routing Enable Strict routing, by default "/foo" and "/foo/" is treated the same by the router
    • view cache Enables view template compilation caching, enabled in production by default
    • view engine The default engine extension to use when omitted
    • views The view directory path, defaulting to "PROCESS.CWD () + '/views '"
    • x-powered-by Enables X-Powered-By: Express the HTTP header, enabled by default

You can also customize the parameters

Assigns setting name to value .

App.set (' title ', ' My Site '); App.get (' title '); // = "My Site"

App.get (name)

Get setting name value.

App.get (' title '); // = undefined app.set (' title ', ' My Site '); App.get (' title '); // = "My Site"



App.use ([path], function)

Use the given middleware function (with optional Mount path , defaulting to "/").

varExpress = require ('Express');varApp =Express ();//Simple LoggerApp.use (function (req, res, next) {Console.log ('%s%s', Req.method, Req.url); Next ();});//RespondApp.use (function (req, res, next) {Res.send ('Hello World');}); App.listen ( the);
将指定的中间件挂在到路径上,默认的路径是"/"


The "mount" path is stripped and was not visible to the middleware function . This feature was mainly to ensure that mounted middleware may operate without code changes, regardless of the "prefix" Path Name.

Here ' s a concrete example. Take the typical use-case of serving files in./public using the express.static() middleware:

 
‘/public‘));
Specify the file path, equivalent to the Windows environment settings
Assuming that the above code is set, you can access the Localhost:3000/photo.jpg equivalent to direct access
Localhost:3000/public/photo.jpg
而这个访问的内部是隐藏起来的,你是无法知道真实的路径的 这样可以确保代码经过修改后无需关注前缀名称
下面是更加详尽的代码解释
 var  logger = require ( " morgan   "  static  (__dirname +       hello     
App.use (Express.  Static'/public'); App.use (Logger ()); App.use (function (req, res) {  Res.send ('Hello');});

Test Access Http://localhost:3000/11.jpg

The first paragraph above uses the middleware to display the path get/11.jpg, while the second code does not show

App.use (express.static (__dirname + '/public '+ '/files '+ '/uploads '));
/public will take precedence over several other

Novice's express4.0 Note API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.