node_express

https://www.cnblogs.com/zapple/p/5683016.html

Emphasize a few points:

An Express application is essentially a stack of middleware which are executed serially.(express应用其实就是由一系列顺序执行的Middleware组成。)
A middleware is a function with access to the request object (req), the response object (res), and the next middleware in line in the request-response cycle of an Express application. It is commonly denoted by a variable named next. Each middleware has the capacity to execute any code, make changes to the request and the reponse object, end the request-response cycle, and call the next middleware in the stack. Since middleware are execute serially, their order of inclusion is important.
If the current middleware is not ending the request-response cycle, it is important to call next() to pass on the control to the next middleware, else the request will be left hanging.
With an optional mount path, middleware can be loaded at the application level or at the router level. Also, a series of middleware functions can be loaded together, creating a sub-stack of middleware system at a mount point.

1, the intermediate route and the nature express req and res are processed and a url path function prototype is as follows:
function (req, res, Next)
both even be mixed, e.g. 4.x scaffolding tool the generated code, the intermediate router used as an object, then the sub-path intermediate router only handles the matching binding path (and corresponding Http request in a manner consistent method).
The difference is that, generally for common routing HTTP method, such as get, post, etc., middleware method distinguish not only for the treatment url, it is the root of the default path. Then, the routing processing only exact match url, url and middleware processing sub-path will match.
Both callback return a response may be determined (or the like req.end req.send api), or specify a callback call to the next, neither can not return, nor next (pause request will cause the leading end page display Not responding).
The execution order of both is in the order written.

express the object can be specified for the common method for routing HTTP, url path may be a regular expression:
app.METHOD (path, the callback [, the callback ...])
app.get ( '/ Example / C', [CB0, CBl, CB2]);
app.get ( '/ Example / C', function (), function ());
If you specify a plurality of the callback, the routing configuration handle, can call the callback next skip the remaining handle.

2, static server
app.use (express.static (path.join (__ dirname , 'public')));
This line of code in the public directory under the file directory as static HelloExpress static middleware to process HTTP URI corresponding for"/". static method may also be provided options specified cache time.
app.use ( '/ static', express.static (path.join (__ dirname, 'public')));
the above code it using the public directory for a static process middleware / static path.

3, Router subject
Express also provides an object called Router, behavior like middleware, you can pass directly to the Router app.use, like the use of middleware that use Router. In addition you can also use the router to handle routing for GET, POST, etc., you can also use it to add middleware, in short, you can Router as a miniature version of the app.
Create a Router instance, then you can use the same router as using app, 4.x the code generated by the scaffold, the router is used as intermediate objects:
var = Express the require ( 'Express');
var = express.Router router ([Options]);
router.get ( '/ Events', function (REQ, RES, Next) {
// ..
});
// for the any Requests the Invoked passed to the this Router
router.use (function (REQ, RES , Next) {
// .. .. some like the any Logic here Wallpaper OTHER Middleware
Next ();
});
module.exports = Router;
the following is in reference to app.js index.js code:
var = routes the require ( ' ./routes/index ');
...
app.use (' / ', routes);
Here the route mounted to the root path, the path must first pay attention to matching the upper middleware, have a chance to match the underlying routing path.

 

Guess you like

Origin www.cnblogs.com/ccdat/p/11694726.html