业务描述:
1。web前端发送http请求
2。web后端为https协议
如何保证web前端发送http请求到web后端(跨域问题:域名不一致即跨域),
因此用node.js写了个代理服务,转发前端http请求到后端,再获取后端数据返回给前端。
代码如下所示:
1 'use strict'; 2 3 const express = require('express'); 4 const fs = require('fs'); 5 const path = require('path'); 6 const https = require('https'); 7 const http=require('http'); 8 const cookieParser = require('cookie-parser'); 9 const bodyParser = require('body-parser'); 10 const app = express(); 11 const url = require("url"); 12 app.use(cookieParser()); 13 app.use(bodyParser.urlencoded({ 14 extended: true 15 })); 16 app.use(bodyParser.json()); 17 18 //设置跨域访问 19 app.all('*', function(req, res, next) { 20 res.header("Access-Control-Allow-Origin", "http://localhost:8080"); 21 res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); 22 res.header('Access-Control-Allow-Credentials', true); 23 res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); 24 res.header("X-Powered-By", ' 3.2.1'); 25 res.header("Content-Type", "application/json;charset=utf-8"); 26 if (req.method == "OPTIONS") res.send(200); 27 else next(); 28 }); 29 // Https代理请求 30 app.post('/rancher', function(req1, res1) { 31 let urlbody=url.parse(req1.body.url) 32 console.log("urlbody========="); 33 console.log(urlbody); 34 let postData =req1.body.postData||''; 35 var opts = { 36 method:req1.body.type, 37 host:urlbody.host, 38 port:urlbody.port||'', 39 // path:urlbody.path+(urlbody.search||''), 40 path:urlbody.path, 41 headers:{ 42 'Content-Type':'application/json', 43 'Content-Length':postData.length, 44 'Authorization':'Basic xx', 45 'Cookie':'', 46 'Cluster-id':req1.body.cid||'' 47 }, 48 rejectUnauthorized:false 49 } 50 console.log("opts========="); 51 console.log(opts); 52 var req = https.request(opts,function(res){ 53 var datas = []; 54 var size = 0; 55 res.on('data',function(data){ 56 datas.push(data); 57 size += data.length; 58 }) 59 res.on('end', function(){ 60 var buff = Buffer.concat(datas, size); 61 console.log('success'+datas); 62 res1.send(buff); 63 }) 64 }) 65 req.on('error',function(err){ 66 console.log('异常,异常原因'+err); 67 }) 68 if (req1.body.type!="get") { 69 req.write(postData); 70 } 71 req.end(); 72 73 }); 74 75 // Http代理请求 76 app.post('/http/rancher', function(req, res1) { 77 let urlbody=url.parse(req.body.url) 78 console.log("urlbody========="); 79 console.log(urlbody); 80 let postData =req.body.postData||''; 81 //发送 http Post 请求 82 var opts = { 83 method:req.body.type, 84 host:urlbody.hostname, 85 port:urlbody.port&&urlbody.port||'', 86 path:urlbody.path, 87 headers:{ 88 'Content-Type':'application/json', 89 'Content-Length':postData.length, 90 'Authorization':'Basic xx', 91 'Cookie': 'xx', 92 }, 93 } 94 var req=http.request(opts, function(res) { 95 console.log('Opts:',opts); 96 console.log('Status:',res.statusCode); 97 console.log('headers:',JSON.stringify(res.headers)); 98 res.setEncoding('utf-8'); 99 res.on('data',function(chun){ 100 console.log('body分隔线--------------------------------- '); 101 console.info(chun); 102 res1.send(chun); 103 }); 104 res.on('end',function(){ 105 console.log('No more data in response.********'); 106 }); 107 }); 108 req.on('error',function(err){ 109 console.error(err); 110 }); 111 req.write(postData); 112 req.end(); 113 }); 114 115 let server = app.listen(8089, function() { 116 let host = server.address().address; 117 let port = server.address().port; 118 119 console.log('Example app listening at http://%s:%s', host, port); 120 });