app.get()特漩、app.use()捻悯、route.get()
1男旗、app.get是app.use的特定方法(app.post),app.use可以支持多個(gè)路由配置
app.use(path,callback)中的callback既可以是router對象又可以是函數(shù)
app.get(path,callback)中的callback只能是函數(shù)
2球散、app做統(tǒng)一的調(diào)用管理巨缘,以實(shí)現(xiàn)中間件和路由的分離添忘。 router視為一個(gè)路由分支,有中間件和HTTP方法路由(如get若锁,put搁骑,post,等),把router當(dāng)作一個(gè)應(yīng)用程序仲器。
router實(shí)現(xiàn)路由模塊化煤率,適合處理路由復(fù)雜的業(yè)務(wù)
//index.js
module.exports = function(app) {
app.get('/', function(req, res){
res.redirect('/posts')
// res.send(1111)
})
app.use('/posts', require('./posts'))
}
//posts.js
const express = require('express')
const router = express.Router()
router.get('/', function(req, res) {
console.log(1111)
// res.send('1111')
res.render('posts',{})
})
router.get('/add', function(req, res) {
console.log(1111)
// res.send('1111')
})
module.exports = router