詳解用node.js實現(xiàn)簡單的反向代理
更新時間:2017年06月26日 11:52:11 作者:JUN_API
本篇文章主要介紹了詳解用node.js實現(xiàn)簡單的反向代理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
之前用node.js實現(xiàn)簡單的反向代理,最近需要回顧,就順便發(fā)到隨筆上了
不多說直接上代碼!
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 = '';
// 封裝獲取參數(shù)的方法
function getParmas(oUrl) {
let oQuery = (typeof oUrl === "object") ? oUrl : url.parse(oUrl, true).query,
data = {};
for (item in oQuery) {
if (item !== 'hostname') {
if (item !== 'path') {
data[item] = oQuery[item];
}
}
}
return querystring.stringify(data);
};
// 封裝發(fā)起http請求的方法
function httpRequest(options, ores) {
let datas = "";
return http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
// 返回數(shù)據(jù)
datas += chunk;
});
res.on('end', function() {
ores.writeHead(200, {
"Content-Type": "application/json; charset = UTF-8",
"Access-Control-Allow-Origin": "*"
});
ores.end(datas);
})
})
};
// 數(shù)據(jù)塊接收中
console.log(oreq.method.toUpperCase());
if (oreq.method.toUpperCase() === "POST") {
console.log("進入POST");
oreq.on("data", function(postDataChunk) {
postData += postDataChunk;
});
// 數(shù)據(jù)接收完畢,執(zhí)行回調(diào)函數(shù)
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"
};
// 發(fā)送請求
let req = httpRequest(options, ores);
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(postData); //發(fā)送請求數(shù)據(jù)
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"
};
// 發(fā)送請求
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');
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
node作為中間服務層如何發(fā)送請求(發(fā)送請求的實現(xiàn)方法詳解)
node作為中間服務層如何發(fā)送請求?下面小編就為大家分享一下發(fā)送請求的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助2018-01-01
node.js中module.exports與exports用法上的區(qū)別
Node.js 引入了模塊(Module)概念,一個模塊可以通過module.exports 或 exports 將函數(shù)、變量等導出,以使其它 JavaScript 腳本通過require() 函數(shù)引入并使用。那么node.js中module.exports與exports有什么區(qū)別呢?下面小編給大家解答下2016-09-09
nodejs入門教程二:創(chuàng)建一個簡單應用示例
這篇文章主要介紹了nodejs入門教程之創(chuàng)建一個簡單應用的方法,涉及nodejs http模塊的引用、端口監(jiān)聽等相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
nest.js,egg.js,midway,express,koa的區(qū)別小結(jié)
本文主要介紹了nest.js,egg.js,midway,express,koa的區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05
總結(jié)Node.js中9種fs模塊文件操作方法(文件夾遞歸刪除知識)
這篇文章主要介紹了總結(jié)Node.js中9種fs模塊文件操作方法(文件夾遞歸刪除知識),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07

