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是什么?

慕侠2389804
浏览 436回答 1
1回答

天涯尽头无女友

这个是get请求发送过来后,use可以接受所有类型的请求,然后将数据带到下一个路由get中,其实use和get这两个路由都会走一遍,next就是自动执行下一个路由,链式反应
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript