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