首頁 > 軟體

如何用nodejs搭建代理伺服器

2022-08-03 22:01:56

代理伺服器的原理

案例

安裝 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()">點選傳送請求</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>

搭建介面伺服器,介面伺服器埠號 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

搭建代理伺服器解決跨域問題

更改 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()">點選傳送請求</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,那麼傳送請求的時候會自動補上主機和埠號http://localhost:3000

所以請求傳送到了3000埠

引數含義

  • target: 轉發到的目標地址
  • changeOrigin: 是否更改host。預設為false,不重寫

true

false

  • pathRewrite:路徑重寫(在這裡是去掉’api’)

最終請求被轉發到了 http://localhost:5000/b

app.get("/b", (req, res) => {
    console.log(req.headers);
    res.send("b");
});

整個過程就像這樣

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com