The difference between app.use () and app.all () of express
var express = require("express");
var app = express();
app.use("/a",function(req,res,next){
console.log("111");
next();
});
app.all("/a",function(req,res,next){
console.log("222");
});
I only know that"/ aura app.use app.use () can print 111l, but app.all () can only recognize"/ ajar.. Is there any difference between them?
what"s the difference between app.use ("/", callback) and app.all ("*", callback))?
searched the difference between app.use () and app.all (), and the answers on the Internet are in the clouds. Please know the answer
use is usually used as middleware
all refers to all request methods in routing, such as all ('/ a'), which can cover: get ('/ a'), post ('/ a'), put ('/ a'), etc.
of course there are previous answers, matching questions.
use as a middleware, use ('/ a') only use the path to match with / a
. For example, / a code b
, / a/b/c
should have a later handler. The most common cases seen by use should be directly use ((.) = > {.})
, and the identification matching path is at the beginning of /
, that is, all.
all is a specific route. If you use a string directly, it matches the path / a
. Only / a
can be matched, and the following request paths are all invalid: / a code b
, / a/b/c
.
app.use ()
accepts one or more callback functions as middleware
. Middleware usually does not handle requests and responses (technically it can). Middleware often processes input data and then passes the request to the next middleware. For example, dealing with header,cookies,sessions and so on.
execution order
in writing order. It must be written before app [method]
, otherwise it will not be executed.
app.use ([path], [callback1 [, callback2]])
, path
defaults /
, which matches the path that starts with /
, and does not accept the regular . The remaining path of the complete path is in the callback function.
app.all (path, [callback1 [, callback2]])
: multiple callback functions can be accepted as route processor
, often dealing with request
and response
. path
matches complete
path, accepts rules, matches all methods, and executes sequentially.
app.all (path,callback1,callback2)
is equivalent to
app.all (path,callback1)
and app.all (path,callback2)
, followed by 2.
Overview:
app.use
is used as middleware, and the calling order is in writing order.
app.all
is used as a routing process, matching the full path, after app.use
. The path of
use
starts with the path, the path of the callback function is the reputation part of the path, and the path of use is complete. The callback only needs to write /
, that is, the full path
executed by the callback is usePath+callbackPath
. The path of the callback function of
all
must be the same as that of app
.
https://stackoverflow.com/que.