首页 > 其他 > 详细

2. 路由

时间:2016-02-15 12:00:47      阅读:114      评论:0      收藏:0      [点我收藏+]

.use适用与为当前路由器加入中间件子路由

.http动词( get、post )方法适用于为当前路由器添加路径处理器

.all匹配任意的http动词方法( get、post方法都会被监听 )

中间件是通过use这种方法添加的

 

1. 关于.use方法

功能一:定义中件( use定义中间件,get是路由对应的中间件(处理函数) ) 

// 路径省略默认为根路径,监听的是根目录下的所有路径
app.use(‘/‘, function(req, res, next) {
    req.myname = ‘leo‘;
    next();
});

app.get(‘/‘, function(req, res) {
    res.send(‘hello world my name is ‘ + req.myname);
});

// 这里的目录相对于app.use里面的目录
app.get(‘/test‘, function(req, res) {
     // 调用响应函数,后面的就不会执行了
    res.send(‘hello world two my name is ‘ + req.myname);
});

功能二:定义子路由

// 这里路径相对于父路径的,http://localhost:3000/myrouter/test
router.get(‘/test‘, function(req, res) {    // 子路由
    res.send(‘sub router test‘);
});

app.use(‘/myrouter‘, router);

 

2. 关于.all

// 匹配所有动词方法,get、post的方法都会被监听
app.all(‘/allpath‘, function(req, res) {
    res.send(‘app.all handle‘);
});

get请求

通过URL访问   http://localhost:3000/allpath

post请求

<form method="post" action="http://localhost:3000/allpath">
    <input type="submit">
</form>

 

2. 路由

原文:http://www.cnblogs.com/zouxinping/p/5190111.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!