zoukankan      html  css  js  c++  java
  • 2. 路由

    .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>
  • 相关阅读:
    多数据源 + Configuration中bean依赖注入顺序问题
    mysql 示例数据库安装
    mysql隔离级别与锁,接口并发响应速度的关系(1)
    management & Actuator
    SpEL
    N/A的含义
    设置U盘图标
    c语言指针
    Decorator(装饰器)
    Lambda Expression
  • 原文地址:https://www.cnblogs.com/zouxinping/p/5190111.html
Copyright © 2011-2022 走看看