express官网里的例子如何理解?

// invoked for any requests passed to this router
router.use(function(req, res, next) {
  // .. some logic here .. like any other middleware
  next();
});

// will handle any request that ends in /events
// depends on where the router is "use()'d"
router.get('/events', function(req, res, next) {
  // ..
});

You can then use a router for a particular root URL in this way separating your routes into files or even mini-apps.

// only requests to /calendar/* will be sent to our "router"
app.use('/calendar', router);

阅读上面代码有一些问题请教:
1.router.get('/events', function(req, res, next) {}); 是将函数映射到/events上,
而app.use('/calendar', router);是将中间件挂载到/calendar上。如果监听的3100端口,我们在浏览器中输入的http://127.0.0.1:3100/calendar,这是不会执行router.get('/events',function(){})中的函数的。上面说的You can then use a router for a particular root URL in this way separating your routes into files or even mini-apps.s这句话该怎么理解?particular root URL是什么?

阅读 1.6k
1 个回答

这个是get请求发送过来后,use可以接受所有类型的请求,然后将数据带到下一个路由get中,其实use和get这两个路由都会走一遍,next就是自动执行下一个路由,链式反应

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题