Node Express中app.use與app.get


app.use

app.use的作用是將一個中間件綁定到應用中,參數path是一個路徑前綴,用於限定中間件的作用范圍,所有以該前綴開始的請求路徑均是中間件的作用范圍,不考慮http的請求方法,例如: 
如果path 設置為’/’,則 
- GET / 
- PUT /foo 
- POST /foo/bar 
均是中間件的作用范圍

 

app.get

app.get是express中應用路由的一部分,用於匹配並處理一個特定的請求,且請求方法必須是GET

app.use('/',function(req, res,next) { res.send('Hello'); next(); });

等同於:

app.all(/^\/.*/, function (req, res) { res.send('Hello'); });

 

實例

app.use('/', function(req, res, next) { res.write(' root middleware'); next(); });

app.use('/user', function(req, res, next) { res.write(' user middleware'); next(); });

app.get('/', function(req, res) { res.end(' /'); });

app.get('/user', function(req, res) { res.end(' user'); });


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM