首页 > 代码库 > express 框架之 路由
express 框架之 路由
先看一段测试代码:
1 var express = require(‘express‘); 2 3 var app = express(); 4 var router = express.Router(); 5 6 app.get(‘/‘, function(req, res){ 7 console.log(‘test1‘); 8 }); 9 10 app.use(‘/‘, function(req, res){11 console.log(‘test2‘);12 });13 14 router.get(‘/‘, function(req, res){15 console.log(‘test3‘);16 });17 18 app.listen(4000);
输入url: localhost:4000
输出结果:test1
输入url: localhost:4000/hello
输出结果:test2
结论:app.get挂载‘/’的路由只响应跟‘/‘精确匹配的GET请求。 而app.use挂载的‘/‘的路由响应所有以‘/‘ 为起始路由的路由,且不限制HTTP访问的方法。以下说明:Mounting a middleware at a path will cause the middleware function to be executed whenever the base of the requested path matches the path.
1 app.use([path], [function...], function)2 Mount the middleware function(s) at the path. If path is not specified, it defaults to "/".3 4 Mounting a middleware at a path will cause the middleware function to be executed whenever the base of the requested path matches the path.
express 框架之 路由
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。