溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用nodejs搭建代理服務(wù)器

發(fā)布時間:2022-08-04 09:35:56 來源:億速云 閱讀:231 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“怎么用nodejs搭建代理服務(wù)器”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么用nodejs搭建代理服務(wù)器”吧!

代理服務(wù)器的原理

怎么用nodejs搭建代理服務(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()">點(diǎn)擊發(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

怎么用nodejs搭建代理服務(wù)器

搭建代理服務(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()">點(diǎn)擊發(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

怎么用nodejs搭建代理服務(wù)器

原理解釋

將 a.html 請求地址改為 /api/b,那么發(fā)送請求的時候會自動補(bǔ)上主機(jī)和端口號http://localhost:3000

怎么用nodejs搭建代理服務(wù)器

怎么用nodejs搭建代理服務(wù)器

所以請求發(fā)送到了3000端口

參數(shù)含義

  • target: 轉(zhuǎn)發(fā)到的目標(biāo)地址

  • changeOrigin: 是否更改host。默認(rèn)為false,不重寫

true

怎么用nodejs搭建代理服務(wù)器

false

怎么用nodejs搭建代理服務(wù)器

  • pathRewrite:路徑重寫(在這里是去掉&rsquo;api&rsquo;)

怎么用nodejs搭建代理服務(wù)器

最終請求被轉(zhuǎn)發(fā)到了 http://localhost:5000/b

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

整個過程就像這樣

怎么用nodejs搭建代理服務(wù)器

到此,相信大家對“怎么用nodejs搭建代理服務(wù)器”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI