当前位置: 首页 > news >正文

网站建设技术外文源码论坛网搭建

网站建设技术外文,源码论坛网搭建,个人专业网站备案,网站制作方案专业乐云seo2019独角兽企业重金招聘Python工程师标准 做前端开发#xff0c;大多数情况下#xff0c;都需要跟后端打交道#xff0c;而最常见的方式则是通过http请求#xff0c;进行通信。 在openresty中#xff0c;通过http跟后端整合通信的方式又很多种#xff0c;各… 2019独角兽企业重金招聘Python工程师标准 做前端开发大多数情况下都需要跟后端打交道而最常见的方式则是通过http请求进行通信。 在openresty中通过http跟后端整合通信的方式又很多种各有各的好处可以根据情况交叉使用 1、直接proxy 这种方式最简单也是我们最熟悉的直接配置一个反向代理跟nginx的用法一致 比如我们有一个后端服务提供用户相关接口是java写的端口8080为了简单起见我直接在openresty里面配置一个server模拟java端通过一个简单的案例的来说明情况 nginx.conf worker_processes 1;error_log logs/error.log;events {worker_connections 1024; }http {lua_package_path /Users/john/opensource/openresty-web-dev/demo7/lua/?.lua;/usr/local/openresty/lualib/?.lua;server {listen 80;server_name localhost;lua_code_cache off;location / {root html;index index.html;}location ~ ^/user {proxy_pass http://127.0.0.1:8080;}}# 这个只是模拟后端server {listen 8080;server_name localhost;lua_code_cache off;location ~ /user/(.) {default_type text/html; content_by_lua_file lua/$1.lua;}}}上面配置了两个location将所有以/user开头的请求都转到后端的8080服务器其他的则是静态页面直接从html目录读取然后返回从这里开始就是前端开发了 为了简单起见假设后端提供了一个登陆接口我们这里直接用lua来实现一下就好了检查用户名跟密码是admin就返回成功否则返回失败 lua/login.lua local req require req local cjson require cjsonlocal args req.getArgs()local username args[username] local password args[password]local res {}if username admin and password admin thenres[ret] trueres[token] ngx.md5(admin/ .. tostring(ngx.time())) elseres[ret] false endngx.say(cjson.encode(res))index.html html headmeta charsetUTF-8titleLogin Page/title /head bodyUserName: input typetext idusername valueadminPassword: input typepassword idpassword valueadmina hrefjavascript:void(0) onclicklogin()Login/ascript src//cdn.bootcss.com/jquery/2.2.4/jquery.min.js/scriptscriptfunction login() {var username $(#username).val();var password $(#password).val();$.post(/user/login, {username: username, password: password}, function(res){console.log(res)var msg res.ret ? 登录成功 : 登录失败alert(msg)}, json)}/script /body /html2、使用ngx.location.captrue 这个方法主要用于发送内部请求即请求当前server内的其他location默认会将当前请求的参数带过去也可以手动指定参数GET参数通过args传递post参数通过body传递 如 local req require req local args req.getArgs() GET 调用 local res ngx.location.capture(/user/login, { method ngx.HTTP_GET, args args, }); POST 调用 local res ngx.location.capture(/user/login, { method ngx.HTTP_POST, body ngx.encode_args(args), }); 现在我们自己写一个lua来调用后台接口实现登陆然后对请求做一点处理实现一些额外的逻辑比如在原来的参数上面加上一个from字段 lua/local-login.lua local req require req local cjson require cjsonlocal args req.getArgs()-- GET local res ngx.location.capture(/user/login, {method ngx.HTTP_GET, args args}) -- POST -- local res ngx.location.capture(/user/login, {method ngx.HTTP_POST, body ngx.encode_args(args)})-- print(res.status) -- 状态码if res.status 200 thenlocal ret cjson.decode(res.body)ret[from] localngx.say(cjson.encode(ret)) elseprint(res.body)ngx.say({ret: false, from: local}) end index.html 也需要改一下多加一个按钮调用本地登陆接口 html headmeta charsetUTF-8titleLogin Page/title /head bodyUserName: input typetext idusername valueadminPassword: input typepassword idpassword valueadmina hrefjavascript:void(0) onclicklogin()Login/aa hrefjavascript:void(0) onclicklocal_login()Local Login/ascript src//cdn.bootcss.com/jquery/2.2.4/jquery.min.js/scriptscriptfunction login() {var username $(#username).val();var password $(#password).val();$.post(/user/login, {username: username, password: password}, function(res){console.log(res)var msg res.ret ? 登录成功 : 登录失败alert(msg)}, json)}function local_login() {var username $(#username).val();var password $(#password).val();$.post(/lua/local-login, {username: username, password: password}, function(res){console.log(res)var msg res.ret ? 本地登录成功 : 本地登录失败alert(msg)}, json)}/script /body /html3、第三方模块lua-resty-http 这种方式跟上面那种不同的地方是调用的时候不会带上本地请求的请求头、cookie、以及请求参数不过这也使得请求更纯粹不会带上那些没必要的东西减少数据传输 最后local-login.lua 变成如下 local req require req local cjson require cjson local http require resty.httplocal args req.getArgs()-- GET -- local res ngx.location.capture(/user/login, {method ngx.HTTP_GET, args args})-- POST -- local res ngx.location.capture(/user/login, {method ngx.HTTP_POST, body ngx.encode_args(args)})-- http local httpc http.new() local res httpc:request_uri(http://127.0.0.1:8080/user/login, {method POST,body ngx.encode_args(args),headers {[Accept] application/json,[Accept-Encoding] utf-8,[Cookie] ngx.req.get_headers()[Cookie],[Content-Type] application/x-www-form-urlencoded,} }) httpc:set_keepalive(60)print(res.status) -- 状态码if res.status 200 thenlocal ret cjson.decode(res.body)ret[from] localngx.say(cjson.encode(ret)) elseprint(res.body)ngx.say({ret: false, from: local}) end到此基本上已经能通过openresty做一些前后端的交互了下次介绍怎么使用openresty模板渲染以及搭配react开发前端。 示例代码 参见demo7部分 转载于:https://my.oschina.net/362228416/blog/820754
http://www.zqtcl.cn/news/727659/

相关文章:

  • 网站做icp备案需要多久上海人才引进官网
  • 国外的设计网站app有什么好的免费网站做教育宣传语
  • 做期货都看那些网站淮北网
  • 网站建设的需求怎么写网站头条怎么做
  • 宜春seoseo网站自动推广
  • 张家界酒店网站建设人人设计网网址
  • 电脑系统做的好的网站什么网站做一手房好
  • 为什么用MyEclipse做网站上海境外输入
  • 做的比较好的小众网站go 是做网站的吗
  • 手机网站快速建设网站接入支付宝需要网站备案吗
  • 贵州省住房城乡建设厅网站农业营销型网站源码
  • 网站开发使用哪种语言wordpress 免费主机
  • 山东免费网站制作绿色食品网站模板
  • 做搜狗网站优化点广州网站开发人
  • 网站建设违法行为广东seo快速排名
  • 体育彩票网站开发该做哪些步骤深圳网站建设策划方案
  • 金华网站建设电话做网站用虚拟机还是服务器
  • 整容医院网站建设目的顺企网贵阳网站建设
  • 微网站 htmlseo做的好的网站
  • 免费做网站推荐东平网页设计
  • 所有复刻手表网站wordpress 标题简码
  • 云南建设厅建设网站首页网站建设s
  • 网站用户需求报告网站充值怎么做的
  • 找代码的网站有一个网站是做釆购的是什么网
  • 做外贸最好的网站有哪些php网站开发工程师待遇
  • 做推文封面的网站首页>新闻>正文 网站怎么做
  • 黄页推广引流网站企业网站导航菜单
  • 合肥专门做网站的公司广告代理商是什么意思
  • wordpress显示一个类目seo推广
  • 营销型电子商务网站特点如何申请免费空间和域名