zoukankan      html  css  js  c++  java
  • 多级缓存-OpenResty获取请求参数

    OpenResty提供了各种API用来获取不同类型的请求参数:

     

    在查询商品信息的请求中,通过路径占位符的方式,传递了商品id到后台:

     需求:在OpenResty中接收这个请求,并获取路径中的id信息,拼接到结果的json字符串中返回

    nginx.conf配置:

    #user  nobody;
    worker_processes  1;
    error_log  logs/error.log;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        
        #加载lua 模块
        lua_package_path "D:/dev/openresty-1.19.9.1/lualib/?.lua;;";  
        #加载c模块     
        lua_package_cpath "D:/dev/openresty-1.19.9.1/lualib/?.so;;"; 
    
        server {
            listen       80;
            server_name  localhost;
            
            location ~ /api/item/(d+) {
                # 响应类型,这里返回json
                default_type application/json;
                # 响应数据由 lua/item.lua这个文件来决定
                content_by_lua_file lua/item.lua;
            }
            location / {
                root   html;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }

     

    编写item.lua文件:

    --获取路径参数
    local id=ngx.var[1]
    
    
    --返回结果
    ngx.say('{"id":'..id..',"name":"SALSA AIR}')

     

    请求接口
    http://localhost/api/item/1001
    {"id":1001,"name":"SALSA AIR}

    http://localhost/api/item/1002
    {"id":1002,"name":"SALSA AIR}

    注意id值随着请求参数变化。

  • 相关阅读:
    VBScript的参数引用
    异常处理规范
    测试感悟
    URL重写
    避免重复记录
    EJB 异常处理探试法
    EJB 异常处理的最佳做法(1)
    使用vbscript脚本调用web服务
    用Dom4j解析XML及中文问题
    lucene简介
  • 原文地址:https://www.cnblogs.com/linjiqin/p/15430371.html
Copyright © 2011-2022 走看看