美文网首页
Difference Between app.use() and

Difference Between app.use() and

作者: 炫海神鹰 | 来源:发表于2018-09-04 11:57 被阅读0次

router.get is only for defining subpaths. Consider this example:

var router = express.Router();
app.use('/first', router);  // Mount the router as middleware at path /first
router.get('/sud', smaller);
router.get('/user', bigger);
  • If you open /first/sud, then the smaller function will get called.
  • If you open /first/user, then the bigger function will get called.
    In short, app.use('/first', router) mounts the middleware at path /first, then router.get sets the subpath accordingly(相应的).

But if we instead use the following:

app.use('/first', fun);
app.get('/sud', bigger);
app.get('/user', smaller);
  • If you open /first in your browser, fun will get called,
  • For /sud, bigger will get called
  • For /user, smaller will get called
    But remember for /first/sud, no function will get called.

相关文章

网友评论

      本文标题:Difference Between app.use() and

      本文链接:https://www.haomeiwen.com/subject/ovdgwftx.html