用node服务器做接口中转,Node做中转服务器,转发接口

1 var http = require("http");2 var url = require("url");3 var fs = require('fs');4 const querystring = require("querystring");5 var path = require('path');6 var formidable = require('formidable'),7 os = require('os'),8 util = require('util');9

10 var config = require('./config').types; //11

12 var netServerUrlFlag = require('./config').netServerUrlFlag;13 var netServerhost = require('./config').netServerhost;14 var netServerport = require('./config').netServerport;15

16 var javaServerUrlFlag = require('./config').javaServerUrlFlag;17 var javaServerhost = require('./config').javaServerhost;18 var javaServerport = require('./config').javaServerport;19

20 var fileServerUrlFlag = require('./config').fileServerUrlFlag;21

22 var webapp = require('./config').webapp;23 var PORT = require('./config').webport;24 /**25 * 上传文件26 * @param files 经过formidable处理过的文件27 * @param req httpRequest对象28 * @param postData 额外提交的数据29 */

30 functionuploadFile(files, req, postData) {31 var boundaryKey = Math.random().toString(16);32 var endData = '

----' + boundaryKey + '--';33 var filesLength = 0, content;34

35 //初始数据,把post过来的数据都携带上去

36 content = (function(obj) {37 var rslt =[];38 Object.keys(obj).forEach(function(key) {39 arr = ['

----' + boundaryKey + '

'];40 arr.push('Content-Disposition: form-data; name="' + obj[key][0] + '"

');41 arr.push(obj[key][1]);42 rslt.push(arr.join(''));43 });44 return rslt.join('');45 })(postData);46

47 //组装数据

48 Object.keys(files).forEach(function(key) {49 if (!files.hasOwnProperty(key)) {50 deletefiles.key;51 return;52 }53 content += '

----' + boundaryKey + '

' +

54 'Content-Type: application/octet-stream

' +

55 'Content-Disposition: form-data; name="' + files[key][0] + '"; ' +

56 'filename="' + files[key][1].name + '";

' +

57 'Content-Transfer-Encoding: binary

';58 files[key].contentBinary = new Buffer(content, 'utf-8');;59 filesLength += files[key].contentBinary.length + fs.statSync(files[key][1].path).size;60 });61 req.setHeader('Content-Type', 'multipart/form-data; boundary=--' +boundaryKey);62 req.setHeader('Content-Length', filesLength +Buffer.byteLength(endData));63

64 //执行上传

65 var allFiles =Object.keys(files);66 var fileNum =allFiles.length;67 var uploadedCount = 0;68 allFiles.forEach(function(key) {69 req.write(files[key].contentBinary);70 console.log("files[key].path:" + files[key][1].path);71 var fileStream = fs.createReadStream(files[key][1].path, { bufferSize: 4 * 1024});72 fileStream.on('end', function() {73 //上传成功一个文件之后,把临时文件删了

74 fs.unlink(files[key][1].path);75 uploadedCount++;76 if (uploadedCount ==fileNum) {77 //如果已经是最后一个文件,那就正常结束

78 req.end(endData);79 }80 });81 fileStream.pipe(req, { end: false});82 });83 }84 var server = http.createServer(function(request, response) {85 var clientUrl =request.url;86 var url_parts = url.parse(clientUrl); //解析路径

87 var pathname =url_parts.pathname;88

89 var sreq =request;90 var sres =response;91 //.net 转发请求

92 if (pathname.match(netServerUrlFlag) != null) {93 var clientUrl2 = clientUrl.replace("/" + netServerUrlFlag, '');94 console.log(".net转发请求......" +clientUrl2);95 var pramsJson = '';96 sreq.on("data", function(data) {97 pramsJson +=data;98 }).on("end", function() {99 var contenttype = request.headers['content-type'];100 if (contenttype == undefined || contenttype == null || contenttype == '') {101 var opt ={102 host: netServerhost, //跨域访问的主机ip

103 port: netServerport,104 path: clientUrl2,105 method: request.method,106 headers: {107 'Content-Length': Buffer.byteLength(pramsJson)108 }109 }110 } else{111 var opt ={112 host: netServerhost, //跨域访问的主机ip

113 port: netServerport,114 path: clientUrl2,115 method: request.method,116 headers: {117 'Content-Type': request.headers['content-type'],118 'Content-Length': Buffer.byteLength(pramsJson)119 }120 }121 }122 console.log('method', opt.method);123

124 var body = '';125 var req = http.request(opt, function(res) {126 res.on('data', function(data) {127 body +=data;128 }).on('end', function() {129 response.write(body);130 response.end();131 });132 }).on('error', function(e) {133 response.end('内部错误,请联系管理员!MSG:' +e);134 console.log("error: " +e.message);135 })136 req.write(pramsJson);137 req.end();138 })139

140 } else

141 //java 转发请求

142 if (pathname.match(javaServerUrlFlag) != null) {143 response.setHeader("Content-type", "text/plain;charset=UTF-8");144

145 var clientUrl2 = clientUrl.replace("/" + javaServerUrlFlag, '');146 console.log(".java转发请求......http://" + javaServerhost + ":" + javaServerport + "" +clientUrl2);147 var prams = '';148 sreq.on("data", function(data) {149 prams +=data;150 }).on("end", function() {151 console.log("client pramsJson>>>>>" +prams);152 const postData =prams;153 console.log("client pramsJson>>>>>" +postData);154 var contenttype = request.headers['content-type'];155 if (contenttype == undefined || contenttype == null || contenttype == '') {156 var opt ={157 host: javaServerhost, //跨域访问的主机ip

158 port: javaServerport,159 path: "/hrrp" +clientUrl2,160 method: request.method,161 headers: {162 'Content-Length': Buffer.byteLength(postData)163 }164 }165 } else{166 var opt ={167 host: javaServerhost, //跨域访问的主机ip

168 port: javaServerport,169 path: "/hrrp" +clientUrl2,170 method: request.method,171 headers: {172 'Content-Type': request.headers['content-type'],173 'Content-Length': Buffer.byteLength(postData)174 }175 }176 }177 var body = '';178

179 console.log('method', opt.method);180 var req = http.request(opt, function(res) {181 //console.log("response: " + res.statusCode);

182 res.on('data', function(data) {183 body +=data;184 }).on('end', function() {185 response.write(body);186 response.end();187 //console.log("end:>>>>>>>" + body);

188 });189 }).on('error', function(e) {190 response.end('内部错误,请联系管理员!MSG:' +e);191 console.log("error: " +e.message);192 })193

194 req.write(postData);195 req.end();196

197

198 })199 } else if (pathname.match(fileServerUrlFlag) != null) {200 //文件拦截保存到本地

201 var form = newformidable.IncomingForm(),202 files =[],203 fields =[];204 form.uploadDir =os.tmpdir();205 form.on('field', function(field, value) {206 console.log(field, value);207 fields.push([field, value]);208 }).on('file', function(field, file) {209 console.log(field, file);210 files.push([field, file]);211 }).on('end', function() {212 //213 var clientUrl2 = clientUrl.replace("/" + fileServerUrlFlag, '');214 var opt ={215 host: netServerhost, //跨域访问的主机ip

216 port: netServerport,217 path: clientUrl2,218 method: request.method219 }220 var body = '';221 var req = http.request(opt, function(res) {222 res.on('data', function(data) {223 body +=data;224 }).on('end', function() {225 response.write(body);226 response.end();227 });228 }).on('error', function(e) {229 response.end('内部错误,请联系管理员!MSG:' +e);230 console.log("error: " +e.message);231 })232 //文件上传

233 uploadFile(files, req, fields);234 });235 form.parse(sreq);236 }237 else{238 var realPath = path.join(webapp, pathname); //这里设置自己的文件名称;

239 var ext =path.extname(realPath);240 ext = ext ? ext.slice(1) : 'unknown';241 fs.exists(realPath, function(exists) {242 //console.log("file is exists:"+exists+" file path: " + realPath + "");

243 if (!exists) {244 response.writeHead(404, {245 'Content-Type': 'text/plain'

246 });247

248 response.write("This request URL " + pathname + " was not found on this server.");249 response.end();250 } else{251 fs.readFile(realPath, "binary", function(err, file) {252 if(err) {253 response.writeHead(500, {254 'Content-Type': 'text/plain'

255 });256 //response.end(err);

257 response.end("内部错误,请联系管理员");258 } else{259 var contentType = config[ext] || "text/plain";260 response.writeHead(200, {261 'Content-Type': contentType262 });263 response.write(file, "binary");264 response.end();265 }266 });267 }268 });269

270 }271 });272 server.listen(PORT);273 console.log("Server runing at port: " + PORT + ".");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值