Node Learning Note 1 Router routing

Source: Internet
Author: User

1. var express = require (' Express '); var app = Express (); App.get ('/', function (req, res) {res.send (' Hello World ');}); App.listen (3000);

One of the most common routes

2. var admin = Express (); Admin.get ('/', function (req, res) {Console.log (Admin.mountpath);//['/adm*n ', '/manager '] res.send (' admin homepage '); }) var secret = Express (); Secret.get ('/', function (req, res) {Console.log (Secret.mountpath);///secr*t Res.send (' Admin secret ');}); Admin.use ('/secr*t ', secret); Load the ' secret ' router on '/secr*t ', the ' admin ' Sub app app.use (['/adm*n ', '/manager '], admin); Load the ' admin ' router on '/adm*n ' and '/manager ', on the parent app

3. App.all (Path, callback [, Callback ...])

App. All(' * ', requireauthentication, loaduser);

App. All(' * ', requireauthentication)

App. All(' * ', loaduser);

App. All('/api/* ', requireauthentication)

is useful for "global" logical mappings for specific path prefixes or arbitrary matches. For example, if you define the top of all the other routes below, it requires that all routes require authentication from then on and automatically loads a user. Remember that these callback functions do not need to be an endpoint: Loaduser can perform a task and then call next () to continue to match subsequent alignments.

4 app.delete (Path, callback [, Callback ...])

App. Delete('/', function (req, res) {

Res. Send(' DELETE request to homepage ');

});

The HTTP delete request is routed to the specified path with the specified callback function. For more information, see Routing Guide. You can provide multiple callback functions that behave just like middleware, except that these callback functions can call the next ("Route") bypass the rest of the route callbacks (s). You can use this mechanism to route prerequisites and then pass control to subsequent alignments if there is no reason to continue the current route.

5.app.disable (name)

Set the Boolean name to False and the name is a table of application settings properties. Call App.set ("foo", false) to call App.disable as a Boolean property ("foo"). For example:

App. Disable(' trust proxy ');

App. Get(' trust proxy ');

= False

6.app.disabled (name)

Returns True if the Boolean setting name is disabled (false), where name is one of The properties from the app settings table.

Returns True if the Boolean setting name is disabled (false), the name is an application set property sheet

App. Disabled(' trust proxy ');

= True

App. Enable(' trust proxy ');

App. Disabled(' trust proxy ');

= False

7.

App.enable (name)

Sets the Boolean setting name to True, where name is one of the properties from the app Settings Table. Calling app.set (' foo ', true) for a Boolean property is the same as calling app.enable (' foo ').

App. Enable(' trust proxy ');

App. Get(' trust proxy ');

= True

app.enabled (name)

Returns True if the setting name is enabled (true), where name is one of the Prope Rties from the app settings table.

App. Enabled(' trust proxy ');

= False

App. Enable(' trust proxy ');

App. Enabled(' trust proxy ');

= True

8.

App.engine (ext, callback)

Registers the given template engine callback as ext.

By default, Express would require () the engine based on the file extension. For example, if you try to render a "foo.jade" file, Express invokes the following internally, and caches the require ( ) on the subsequent calls to increase performance.

App. Engine(' Jade ', require(' Jade '). __express);

Use this method for engines it does not provide . __express out of the box, or if you wish to "map" a different ext Ension to the template engine.

For example, to maps the EJS template engine to ". html" files:

App. Engine(' HTML ', require(' Ejs '). RenderFile);

In this case, EJS provides A. RenderFile () method with the same signature that Express expects: (path, Options , callback), though note that it aliases the This method as ejs.__express internally so if you ' re using ". Ejs" ext Ensions you don ' t need to do anything.

Some template engines do not follow this convention. The consolidate.js Library maps Node template engines to follow this convention, so they work seemlessly with EXP Ress.

var engines = require(' consolidate ');

App. Engine(' Haml ', engines. Haml);

App. Engine(' HTML ', engines. Hogan);

9.

App.get (name)

Returns The value of the name app setting, where name is one of the strings in the app settings table. For example:

App. Get(' title ');

= undefined

App. Set(' title ', ' My Site ');

App. Get(' title ');

= "My Site"

App.get (Path, callback [, callback ...])

Routes HTTP GET requests to the specified path with the specified callback functions. For more information, see the RoutingGuide.

You can provide multiple callback functions the behave just like middleware, except these callbacks can invoke next (' Route ') to bypass the remaining route callback (s). You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there ' s no Reaso N to proceed with the current route.

App. Get('/', function (req, res) {

Res. Send(' GET request to homepage ');

});

10.

Node Learning Note 1 Router routing

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.