之前用node.js实现简单的反向代理,最近需要回顾,就顺便发到随笔上了
不多说直接上代码!
const http = require('http'); const url = require('url'); const querystring = require('querystring'); http.createServer(function(oreq, ores) { console.log("服务已开启"); if (oreq) { if (oreq.url !== '/favicon.ico') { let content = '', postData = ''; // 封装获取参数的方法 function getParmas(oUrl) { let oQuery = (typeof oUrl === "object") ""; return http.request(options, function(res) { res.setEncoding('utf8'); res.on('data', function(chunk) { // 返回数据 datas += chunk; }); res.on('end', function() { ores.writeHead(200, { "Content-Type": "application/json; charset = UTF-8", "Access-Control-Allow-Origin": "*" }); ores.end(datas); }) }) }; // 数据块接收中 console.log(oreq.method.toUpperCase()); if (oreq.method.toUpperCase() === "POST") { console.log("进入POST"); oreq.on("data", function(postDataChunk) { postData += postDataChunk; }); // 数据接收完毕,执行回调函数 oreq.on("end", function() { console.log("接收完毕") console.log(postData); // 配置options let oData = JSON.parse(postData); postData = getParmas(oData); let options = { hostname: oData.hostname, port: '80', path: oData.path, method: "POST" }; // 发送请求 let req = httpRequest(options, ores); req.on('error', function(e) { console.log('problem with request: ' + e.message); }); req.write(postData); //发送请求数据 req.end(); }); } else { let queryObj = url.parse(oreq.url, true).query; content = getParmas(oreq.url); let options = { hostname: queryObj.hostname, port: '80', path: queryObj.path + content, method: "GET" }; // 发送请求 let req = httpRequest(options, ores); req.on('error', function(e) { console.log('problem with request: ' + e.message); }); req.end(); } } } }).listen(8080, '127.0.0.1');
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]
- 670《脆弱敏感小女生》[320K/MP3][27.53MB]
- 曾琳.2011-好好爱我【南方】【WAV+CUE】
- 范晓萱.1995-RAIN【福茂】【WAV+CUE】