当前位置: 首页 > Web前端 > HTML

SAPUI5架构在http-proxy库上的单步调试

时间:2023-04-02 16:10:15 HTML

源代码:constexpress=require('express'),httpProxy=require('http-proxy'),fs=require('fs'),proxy=newhttpProxy.createProxyServer();constappRoute={target:'http://localhost:5000'};constrouting=JSON.parse(fs.readFileSync('./odata.json'));varallowCrossDomain=function(req,res){res.header('Access-Control-Allow-Origin','*');res.header('Access-Control-Allow-Credentials','true');res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE');res.header('Access-Control-Allow-Headers','X-Requested-With,Accept,Origin,Referer,User-Agent,Content-Type,Authorization,X-Mindflash-SessionID');//拦截OPTIONSmethodif('OPTIONS'===req.method){res.header(200);}else{vardirname=req.url.replace(/^\/([^\/]*).*$/,'$1');varroute=routing[目录名]||应用程序;console.log(req.method+':'+route.target+req.url);proxy.web(req,res,route);}};varapp=express();app.use(allowCrossDomain);app.listen(8085);console.log("Proxystartedonhttp://localhost:8085");路由JSON对象解析结果:app.use(allowCrossDomain);setrouter,对于路径/,使用传入的allowCrossDomain函数:然后监听8085端口。我在另一个浏览器窗口访问localhost:8085,在路由配置对象中找不到对应的值:所以使用默认的appRoute。重定向到5000端口:因为没有服务监听5000端口:所以修改proxy.js中的配置。在appRoute中,将target指向真实的SAPUI5服务器运行地址:然后通过代理服务器访问整个应用。这样就隐藏了8080端口,起作用的是代理服务器监听的8085端口。在Chrome开发者工具网络中观察到的都是8085端口:在odata.json文件中添加一个名为sap的配置:{"sap":{"target":"http://10.0.xx.xx"}}所以如果我在地址栏中输入:http://localhost:8085/sap/根据这个正则表达式:vardirname=req.url.replace(/^\/([^\/]*).*$/,'$1');将自动提取url中8085之后出现的对//中的sap。然后执行:varroute=routing[dirname]||应用程序;从odata.json的配置中提取新的路由信息??:从上图可以看出,所有发送到http://localhost:8085/sap/的请求,都会被重定向到10.0.xx.xx/sap。当请求被代理时,它遵循两个不同的管道,将转换应用于req和res对象。第一个管道(传入)负责创建和操作将客户端连接到目标的流。第二个管道(传出)负责创建和操作将数据从目标返回到客户端的流。