大家好,
我现在正在写一个restful的应用。比如一个 新闻的地址 /news
当用Get方法访问的时候是获取 全部的新闻列表,由list.lua处理。
当用Post方法访问的时候是新建一个新闻,由create.lua处理。
我现在的做法是这样的,先声明一个路由地址,2个内部地址用来处理不同的方法。
location = /news {
content_by_lua_file routes.lua;
}
location = @news-by-get {
content_by_lua_file list.lua
}
location = @news-by-post {
content_by_lua_file create.lua
}
再写一个路由脚本routes.lua 里面根据请求方法调用具体的内部地址:
if ngx.req.get_method() == 'GET' then
return ngx.exec("@news-by-get")
end
if ngx.req.get_method() == 'POST' then
return ngx.exec("@news-by-post")
end
不知道这么做对不对,或者哪里需要改进,想听听大家的意见。谢谢。