如何用nodejs搭建代理服務(wù)器
代理服務(wù)器的原理
案例
安裝 express、http-proxy-middleware
app.js 文件 node app.js
var express = require('express'); var app = express(); app.use(express.static('./public')); app.listen(3000);
在 public 文件夾下建立 a.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <button onclick="Click()">點擊發(fā)送請求</button> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <script> function Click() { axios('http://localhost:5000/b') .then(function(res) { console.log(res); }); } </script> </body> </html> </body> </html>
搭建接口服務(wù)器,接口服務(wù)器端口號 5000
node interface.js
var express = require('express'); var app = express(); app.get("/", (req, res) => { res.send("123"); }); app.get("/api/a", (req, res) => { res.send("a"); }); app.get("/b", (req, res) => { console.log(req.headers); res.send("b"); }); app.listen(5000);
訪問http://localhost:3000/a.html
搭建代理服務(wù)器解決跨域問題
更改 app.js
var express = require('express'); var proxy = require('http-proxy-middleware'); var app = express(); app.use(express.static('./public')); app.use('/api', proxy.createProxyMiddleware({ target: 'http://localhost:5000', changeOrigin: false, pathRewrite: { "^/api": "" } })); app.listen(3000);
更改 a.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <button onclick="Click()">點擊發(fā)送請求</button> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <script> function Click() { // axios('http://localhost:5000/b') // .then(function(res) { // console.log(res); // }); axios('/api/b') .then(function(res) { console.log(res); }); } </script> </body> </html> </body> </html>
訪問 http://localhost:3000/a.html
原理解釋
將 a.html 請求地址改為 /api/b,那么發(fā)送請求的時候會自動補上主機和端口號http://localhost:3000
所以請求發(fā)送到了3000端口
參數(shù)含義
target
: 轉(zhuǎn)發(fā)到的目標(biāo)地址changeOrigin
: 是否更改host。默認(rèn)為false,不重寫
true
false
pathRewrite
:路徑重寫(在這里是去掉’api’)
最終請求被轉(zhuǎn)發(fā)到了 http://localhost:5000/b
app.get("/b", (req, res) => { console.log(req.headers); res.send("b"); });
整個過程就像這樣
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
node.js實現(xiàn)http服務(wù)器與瀏覽器之間的內(nèi)容緩存操作示例
這篇文章主要介紹了node.js實現(xiàn)http服務(wù)器與瀏覽器之間的內(nèi)容緩存操作,結(jié)合實例形式分析了node.js http服務(wù)器與瀏覽器之間的內(nèi)容緩存原理與具體實現(xiàn)技巧,需要的朋友可以參考下2020-02-02Node.js創(chuàng)建子進(jìn)程的幾種實現(xiàn)方式
這篇文章主要介紹了Node.js創(chuàng)建子進(jìn)程的幾種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10Node.js readline 逐行讀取、寫入文件內(nèi)容的示例
本篇文章主要介紹了Node.js readline逐行讀取、寫入文件內(nèi)容的示例,運用readline逐行讀取的兩種實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03