說(shuō)說(shuō)如何利用 Node.js 代理解決跨域問(wèn)題
前后端分離,經(jīng)常會(huì)出現(xiàn)跨域訪問(wèn)被限制的問(wèn)題。
跨域訪問(wèn)限制是服務(wù)端出于安全考慮的限制行為。即只有同域或者指定域的請(qǐng)求,才能訪問(wèn)。這樣還可以防止圖片被盜鏈。服務(wù)端(比如 Node.js)可以通過(guò)代理,來(lái)解決這一問(wèn)題。
1 安裝 request 庫(kù)
npm install request --save-dev
2 配置
我們以知乎日?qǐng)?bào)為例,配置兩個(gè)代理。一個(gè)代理內(nèi)容,另一個(gè)代理圖片。
在項(xiàng)目根目錄,配置 proxy.js :
//代理
const http = require('http');
const request = require('request');
const hostIp = '127.0.0.1';
const apiPort = 8070;
const imgPort = 8071;
//創(chuàng)建 API 代理服務(wù)
const apiServer = http.createServer((req, res) => {
console.log('[apiServer]req.url='+req.url);
const url = 'http://news-at.zhihu.com/story' + req.url;
console.log('[apiServer]url='+url);
const options = {
url: url
};
function callback(error, response, body) {
if (!error && response.statusCode === 200) {
//編碼類(lèi)型
res.setHeader('Content-Type', 'text/plain;charset=UTF-8');
//允許跨域
res.setHeader('Access-Control-Allow-Origin', '*');
//返回代理內(nèi)容
res.end(body);
}
}
request.get(options, callback);
});
//監(jiān)聽(tīng) API 端口
apiServer.listen(apiPort, hostIp, () => {
console.log('代理接口,運(yùn)行于 http://' + hostIp + ':' + apiPort + '/');
});
//創(chuàng)建圖片代理服務(wù)
const imgServer = http.createServer((req, res) => {
const url = 'https://pic2.zhimg.com/' +req.url.split('/img/')[1];
console.log('[imgServer]url=' + url);
const options = {
url: url,
encoding: null
};
function callback(error, response, body) {
if (!error && response.statusCode === 200) {
const contentType = response.headers['content-type'];
res.setHeader('Content-Type', contentType);
res.setHeader('Access-Control-Allow-Origin', '*');
res.end(body);
}
}
request.get(options, callback);
});
//監(jiān)聽(tīng)圖片端口
imgServer.listen(imgPort, hostIp, () => {
console.log('代理圖片,運(yùn)行于 http://' + hostIp + ':' + imgPort + '/')
});
代理的關(guān)鍵點(diǎn)是在響應(yīng)頭添加 Access-Control-Allow-Origin 為 *,表示允許所有域進(jìn)行訪問(wèn)。
| 代理前地址 | 代理后地址 |
|---|---|
| https://pic2.zhimg.com/v2-bb0a0282fd989bddaa245af4de9dcc45.jpg | http://127.0.0.1:8071/img/v2-bb0a0282fd989bddaa245af4de9dcc45.jpg |
| http://news-at.zhihu.com/story/9710345 | http://127.0.0.1:8070/9710345 |
3. 運(yùn)行
執(zhí)行:
node proxy.js
運(yùn)行結(jié)果:
代理接口,運(yùn)行于 http://127.0.0.1:8070/代理圖片,運(yùn)行于http://127.0.0.1:8071/
打開(kāi)瀏覽器,輸入代理后的地址,就可以正常訪問(wèn)啦O(∩_∩)O哈哈~
代理內(nèi)容:

代理圖片:

以上所述是小編給大家介紹的Node.js代理解決跨域問(wèn)題詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
node.js基于express使用websocket的方法
這篇文章主要介紹了node.js基于express使用websocket的方法,結(jié)合實(shí)例形式分析了node.js基于express調(diào)用websocket相關(guān)設(shè)置與使用操作技巧,需要的朋友可以參考下2017-11-11
Node.js如何實(shí)現(xiàn)注冊(cè)郵箱激活功能 (常見(jiàn))
今天了解了node如何實(shí)現(xiàn)郵箱激活功能,這個(gè)功能非常常見(jiàn),當(dāng)我們注冊(cè)一個(gè)賬號(hào)時(shí),肯定會(huì)有這步,下面看下如何實(shí)現(xiàn)這個(gè)功能2017-07-07
Node定時(shí)備份MySQL的實(shí)現(xiàn)
本文主要介紹了Node定時(shí)備份MySQL的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

