這篇文章給大家分享的是有關(guān)怎么用node.js實現(xiàn)簡單的反向代理的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

1、能夠嵌入動態(tài)文本于HTML頁面。2、對瀏覽器事件做出響應(yīng)。3、讀寫HTML元素。4、在數(shù)據(jù)被提交到服務(wù)器之前驗證數(shù)據(jù)。5、檢測訪客的瀏覽器信息。6、控制cookies,包括創(chuàng)建和修改等。7、基于Node.js技術(shù)進(jìn)行服務(wù)器端編程。
代碼如下:
const http = require('http');
const url = require('url');
const querystring = require('querystring');
http.createServer(function(oreq, ores) {
console.log("服務(wù)已開啟");
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("進(jìn)入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');感謝各位的閱讀!關(guān)于“怎么用node.js實現(xiàn)簡單的反向代理”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
文章題目:怎么用node.js實現(xiàn)簡單的反向代理-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://www.chinadenli.net/article26/dcpijg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站制作、網(wǎng)站設(shè)計公司、小程序開發(fā)、網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容