zoukankan      html  css  js  c++  java
  • lapis http verb 处理

    1. 同一个url 包含不同的请求(respond_to  进行解决)
    // 路由格式 match ,通过respond_to 进行实际的http verb 处理
    
    local lapis = require("lapis")
    local respond_to = require("lapis.application").respond_to
    local app = lapis.Application()
    
    app:match("create_account", "/create-account", respond_to({
      GET = function(self)
        return { render = true }
      end,
      POST = function(self)
        do_something(self.params)
        return { redirect_to = self:url_for("index") }
      end
    }))
    
    备注 respond_to 可以包含一个before 的filter 进行一个格外的权限,参数处理
    如下:
    app:match("edit_user", "/edit-user/:id", respond_to({
      before = function(self)
        self.user = Users:find(self.params.id)
        if not self.user then
          self:write({"Not Found", status = 404})
        end
      end,
      GET = function(self)
        return "Edit account " .. self.user.name
      end,
      POST = function(self)
        self.user:update(self.params.user)
        return { redirect_to = self:url_for("index") }
      end
    }))
    2. 明确指明请求的http verb
    如下:
    
    app:get("/test", function(self)
      return "I only render for GET requests"
    end)
    
    app:delete("/delete-account", function(self)
      -- do something destructive
    end)
    3. http verb 请求的过滤(before filter )
    // 全局控制
    app:before_filter(function(self)
      if not user_meets_requirements() then
        self:write({redirect_to = self:url_for("login")})
      end
    end)
    // respond_to 添加,例子参考上面的
    4. http  request && response API 参考
    比较多,参考文档 http://leafo.net/lapis/reference/actions.html#routes-and-url-patterns/route-precedence
    主要是一些 session cookies url  处理
    5. http  render 参数
    参考格式
    app:match("/", function(self)
      return { render = "error", status = 404}
    end)
    
    参数选项
    status —  http 状态码
    render —  渲染的模板名称
    content_type — 内容类型
    headers —  http  header 参数
    json —  指定返回的数据是json 格式的,既 content_type  application/json
    layout —  指定渲染使用的模板,可以全局设置
    redirect_to —  设置重定向类型 301 302 。。。
    5. 默认请求处理 (默认路由)
    function app:default_route()
      ngx.log(ngx.NOTICE, "User hit unknown path " .. self.req.parsed_url.path)
    
      -- call the original implementaiton to preserve the functionality it provides
      return lapis.Application.default_route(self)
    end
    6. 异常错误处理
    function app:handle_error(err, trace)
      if config.custom_error_page then
        return { render = "my_custom_error_page" }
      else
        return lapis.Application.handle_error(self, err, trace)
      end
    end
    7. 参考文档
    http://leafo.net/lapis/reference/actions.html#routes-and-url-patterns/route-precedence
    https://github.com/leafo/lapis-exceptions
  • 相关阅读:
    xgzc— math 专题训练(一)
    floyd判圈算法
    CF961G Partitions
    luoguP4778 Counting swaps
    AT3913 XOR Tree(巧妙转换+状压dp)
    手动实现aop编程
    代理模式
    spring重点一:处理对象创建时间 个数以及方式
    spring helloword
    spring用来干什么,解决的问题
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/7840839.html
Copyright © 2011-2022 走看看