zoukankan      html  css  js  c++  java
  • express的路由规则

    路由规则

    express 封装了多种 http 请求方式,我们主要只使用 

    get

     和 

    post

     两种。 get 和 post 的第一个参数都为请求的路径,第二个参数为处理请求的回调函数,回调函数有两个参数分别是 req 和 res,代表请求信息和响应信息 。路径请求及对应的获取路径有以下几种形式:

    req.query

    // GET /search?q=tobi+ferret  
    req.query.q  
    // => "tobi ferret"  

    // GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse  
    req.query.order  
    // => "desc"  

    req.query.shoe.color  
    // => "blue"  

    req.query.shoe.type  
    // => "converse"

    req.body

    // POST user[name]=tobi&user[email]=tobi@learnboost.com  
    req.body.user.name  
    // => "tobi"  

    req.body.user.email  
    // => "tobi@learnboost.com"  

    // POST { "name": "tobi" }  
    req.body.name  
    // => "tobi"

    req.params

    // GET /user/tj  
    req.params.name  
    // => "tj"  

    // GET /file/javascripts/jquery.js  
    req.params[0]  
    // => "javascripts/jquery.js"  

    **req.param(name)**

    // ?name=tobi  
    req.param('name')  
    // => "tobi"  

    // POST name=tobi  
    req.param('name')  
    // => "tobi"  

    // /user/tobi for /user/:name   
    req.param('name')  
    // => "tobi"

    不难看出:

    • req.query

      : 处理 get 请求

    • req.params

      : 处理 /:xxx 形式的 get 请求

    • req.body

      : 处理 post 请求

    • req.param()

      : 可以处理 get 和 post 请求,但查找优先级由高到低为req.params→req.body→req.query

    路径规则还支持正则表达式,更多请查阅:http://expressjs.com/api.html

  • 相关阅读:
    从客户端(&)中检测到有潜在危险的 Request.Path 值。
    对访问修饰关键字public, protected, internal and private的说明
    C#综合揭秘——细说多线程(下)
    iis下配置:php+mysql
    工厂模式(Factory Patter)
    HDU 1040 As Easy As A+B [补]
    HDU 1020 Encoding
    HDU 1076 An Easy Task
    UVA 100 The 3n + 1 problem
    民大OJ 1668 追杀系列第二发
  • 原文地址:https://www.cnblogs.com/neights/p/3634508.html
Copyright © 2011-2022 走看看