路由規則是
app.use(path,router)
定義的,
router
表明一個由
express.Router()
建立的對象,在路由對象中可定義多個路由規則。但是若是咱們的路由只有一條規則時,可直接接一個回調做爲簡寫,也可直接使用
app.get
或
app.post
方法。即
當一個路徑有多個匹配規則時,使用app.use,不然使用相應的app.method(get、post)
總結:
app.use用來使用中間件( middleware)
2.Router
A router object is an isolated instance of middleware and routes. You can think of it as a 「mini-application,」 capable only of performing middleware and routing functions. Every Express application has a built-in app router.路由器對象是中間件和路由的孤立實例。 您能夠將其視爲「微型應用程序」,只能執行中間件和路由功能。 每一個Express應用程序都有一個內置的應用程序路由器
A router behaves like middleware itself, so you can use it as an argument to app.use() or as the argument to another router’s use() method.路由器的行爲就像中間件自己,所以您能夠將其用做app.use()的參數或做爲另外一路由器的use()方法的參數。
The top-level express object has a Router() function that creates a new router object.
Create a new router as follows:
var router = express.Router([options]);