\n    

請求轉(zhuǎn)發(fā)測試<\/h1>\n    <\/p>\n    

国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

??
?? ????? ?????
? ??? ??????
??? ??? ??? ???? ??
1. ??? ??
1. ??? ??
2. ?? ??
1.借助JSONP
2.使用CORS
??
? ?? ? ???? ??? ??? ? ??? ???? ??? ?????? ???? ???? ?? ??? ??

??? ? ??? ???? ??? ?????? ???? ???? ?? ??? ??

Apr 25, 2023 pm 07:57 PM
??? ?? ?? nginx

??? ? ??? ???? ??? ?????? ???? ???? ?? ??? ??

??? ???? ?? ???? ?? ??? ??????, ?????? ?? ???? ????? ???. ?? ??? ? ???? ? ?? ?? ??? ??? ?? ???? ??? ?? ??? ???? ?? ??????.

??? ??? ????? ???? ??? ? ??? ???? ? ?? ???? ??? ?? ???? ?????.

?? ????? ?????

?? ???? ?? ????? ?? ?? URL ??? ???????.

URL ???? ????? ????, ??? ??, ?? ??, ??? ?????. , ?? ???? ? ?? ?? ??.

URL? ?? ??? ????.

https://www.example.com:8080/path/resource.html?page=1&sort=desc#header

?? ???:
● ????? HTTPS???.
● ??? ??? www.example.com
● ?? ??? 8080???.
● ??? /path???. /resource.html
● ?? ????? page=1&sort=desc
● ??? header

?? ??? ???? ?? URL? ????, ??? ?? ? ?? ??? ??? ??.

? URL? ?? ?? ??? ?? ?? ??? ??? ????? ?????.

http://www.example.com:8080/    // 協(xié)議不同
https://www.example.a.com:8080/ // 主機(jī)名不同
https://www.example.com:8081/   // 端口號不同

? ??? ??????

?????? ??? ?? ??? ??????. ????? ?? ?? ??? ??.

所謂同源策略,其實(shí)是瀏覽器的一種安全機(jī)制,用于限制一個(gè)網(wǎng)頁中的網(wǎng)絡(luò)請求僅能夠訪問來自同一源(域名、協(xié)議和端口號均相同)的資源,主要目的是防止惡意網(wǎng)站通過腳本竊取其他網(wǎng)站的敏感數(shù)據(jù),保障用戶的隱私和安全。

???? ? ????(js ??)? ?? ???? ???? ???? ????? ??? ? ??? ?????.

??? ??? ??? ???? ??

?? ????? ??? ??? ??? ??? ????? ?? ?? ??? ?? ????? ??? ??? ??? ???? ???? ???? ??? ????? ???? ??????. :

1. ??? ??

??? ???? ???? ??? ? ??? ???? ?? ?? ????? ???? ???? ??? ??? ???? ????.

??? ?? ??? ??? ??? ???? ?? ?? ?? ??? ????? ?? ???? ???? ???? ??? ?? ????? ??? ??? ??? ??? ?????. ?? ???? ?? ?? ??? ????.

????? ????? ????? ??? ????? webpack? ?? ?????? ??? ??? ???? ?????. ????? ????? ?????? ???? ??? ???? ?? ??? ??? ??? ??? ?????. , ??? ?? ??? ?? ??? ???? ?? ???? ??? ??? ?????? ?????.

??? ??? ? ?????? ?? ??? ?? ?? ?? ??? ???? ?? ??? ??? ??, ??? ? ??? ? ????.

????? ????? ??? ???? ???? ??? ?????? ??? ????.

???? ??? ??? ??? ??? ?? ???? ??? ??? ??????

1. ??? ??
??? ????? ?????

nginx? ??? ???? ???? ????? ??? ?? ?????? ?????.

nginx? ??? ??? ? ??, ??? ??, ??? ????? Windows? Linux ???? ??? ? ????.

?? ? ??? ? ??? ???? ?? ??? ??? ???? ?? ??? ??? ??? ?? URL? ?? ?? ??? ??? ?? ?? ??? ??? ??? ?? ? ??? ???? ????.

2. ?? ??
?? ????? webpack?? vite ?? ?? ????? ???? ??? ??? ?? ?????? ??? ? ?? ??? ???

? ???? ?????. http-proxy-middleware ???? . http-proxy-middleware ????? ??? http-proxy? ??? ???? ????.

???

http-proxy-middleware? ???? ?????? ?? ?? ??? ???? ?? ?????.

const { createProxyMiddleware } = require(&#39;http-proxy-middleware&#39;);

module.exports = {
  server: {
    proxy: {
      // 將 /api/* 的請求代理到 http://localhost:3000/*
      &#39;/api&#39;: {
        target: &#39;http://localhost:3000&#39;,
        changeOrigin: true,
        pathRewrite: { &#39;^/api&#39;: &#39;/&#39; }
      }
    }
  }
};

?? ??

??? ?? ??? ?? ???? http-proxy ?????? ???? ?? ??? ??? ? ????. ?? ??? ?? ??, ?? ?? ??? ?? ????? ???? ? ????. :

1. ?? ???? ??? ? ??(?? ??)? ?? ?? npm init -y? ???? ???. ????? ???? ?? ????? ??? ??????????:

npm init -y

2. ?? ?? ???? ?? ????? index.html ??? ??? ?? ??? ??? ?????.

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>請求轉(zhuǎn)發(fā)測試</title>
</head>

<body>
    <h1>請求轉(zhuǎn)發(fā)測試</h1>
    <p id="message"></p>
    <script>
        fetch(&#39;/api/login&#39;)
            .then(response => response.text())
            .then(data => {
                document.getElementById(&#39;message&#39;).textContent = data;
            });
    </script>
</body>

</html>

3. ?? ?? ? ? ????. ??? ??? ???? ?? ???? ?? ???? .js ??? ??????.
index.js ??? ?? ?? ??? ?? ??? ??? ???? ?? ?? ?????.

const http = require(&#39;http&#39;);
const httpProxy = require(&#39;http-proxy&#39;);
const fs = require(&#39;fs&#39;);
const path = require(&#39;path&#39;);

// 創(chuàng)建代理服務(wù)器實(shí)例
const proxy = httpProxy.createProxyServer({});

// 創(chuàng)建HTTP服務(wù)器
const server = http.createServer((req, res) => {
    if (req.url === &#39;/&#39; || req.url.endsWith(&#39;.html&#39;)) {
        // 讀取HTML文件
        const filename = path.join(__dirname, &#39;index.html&#39;);
        fs.readFile(filename, &#39;utf8&#39;, (err, data) => {
            if (err) {
                res.writeHead(500);
                res.end(&#39;Error reading HTML file&#39;);
            } else {
                res.writeHead(200, { &#39;Content-Type&#39;: &#39;text/html&#39; });
                res.end(data);
            }
        });
    } else if (req.url.startsWith(&#39;/api&#39;)) {
        // 重寫路徑,替換跨域關(guān)鍵詞
        req.url = req.url.replace(/^\/api/, &#39;&#39;);
        // 將請求轉(zhuǎn)發(fā)至目標(biāo)服務(wù)器
        proxy.web(req, res, {
            target: &#39;http://localhost:3000/&#39;,
            changeOrigin: true,
        });    
    }
});

// 監(jiān)聽端口
server.listen(8080, () => {
    console.log(&#39;Server started on port 8080&#39;);
});

4. ?? ?? ??? ? ??? ???? ?? ?? ?? target.js ??? ??? ?????.

const http = require(&#39;http&#39;);

const server = http.createServer((req, res) => {
    if (req.url.startsWith(&#39;/login&#39;)) {
        res.writeHead(200, { &#39;Content-Type&#39;: &#39;text/plain&#39; });
        res.end(&#39;我是localhost主機(jī)3000端口下的方法,恭喜你訪問成功!&#39;);
    } else {
        res.writeHead(200, { &#39;Content-Type&#39;: &#39;text/plain&#39; });
        res.end(&#39;Hello, world!&#39;);
    }
});

server.listen(3000, () => {
    console.log(&#39;Target server is listening on port:3000&#39;);
})

5. 打開終端,輸入啟動目標(biāo)服務(wù)器的命令:

node ./target.js //項(xiàng)目根目錄下執(zhí)行

6. 再開一個(gè)終端啟動代理服務(wù)器,等待瀏覽器端發(fā)起請求就可以啦:

node ./index.js //項(xiàng)目根目錄下執(zhí)行

7. 最后在瀏覽器里訪問http://localhost:8080, 打開控制臺即可查看效果:

可以發(fā)現(xiàn),瀏覽器network模塊的網(wǎng)絡(luò)請求確實(shí)是訪問的8080端口的方法,但是我們的服務(wù)器默默的做了請求轉(zhuǎn)發(fā)的功能,并將請求轉(zhuǎn)發(fā)獲取到的內(nèi)容返回到了前端頁面上。

其實(shí)http-proxy是對node內(nèi)置庫http的進(jìn)一步封裝,網(wǎng)絡(luò)請求的核心部分還是使用http創(chuàng)建一個(gè)服務(wù)器對象去訪問的。感興趣的同學(xué)可以再讀讀http-proxy的源碼~

除了代理服務(wù)器這種繞過瀏覽器同源策略的解決方式外,從前端的角度解決跨域問題還有如下一些常見的方法:

1.借助JSONP

JSONP的原理是通過動態(tài)創(chuàng)建

? AI ??

Undresser.AI Undress

Undresser.AI Undress

???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover

AI Clothes Remover

???? ?? ???? ??? AI ?????.

Video Face Swap

Video Face Swap

??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

???

??? ??

???++7.3.1

???++7.3.1

???? ?? ?? ?? ???

SublimeText3 ??? ??

SublimeText3 ??? ??

??? ??, ???? ?? ????.

???? 13.0.1 ???

???? 13.0.1 ???

??? PHP ?? ?? ??

???? CS6

???? CS6

??? ? ?? ??

SublimeText3 Mac ??

SublimeText3 Mac ??

? ??? ?? ?? ?????(SublimeText3)

???

??? ??

??? ????
1601
29
PHP ????
1502
276
???
PHP ??? ??? ? PHP ??? ???? ??? ?????? PHP ??? ???? ? ?? ???? ?? PHP ??? ??? ? PHP ??? ???? ??? ?????? PHP ??? ???? ? ?? ???? ?? May 23, 2025 pm 08:33 PM

PHP ??? ?? ?? ???? ??? ? ????. 1. ?? ?? ???? "PHP ?? ??"? ?? ???? ????? ?????. 2. ??? ?? ?? ????? ?? ? ??? ?? ????? ?? ???????. 3. IDE?? ???? ?? ??? ??? ??????. 4. ??? PHP ?? ?? ?? ?? ?? ???? ???? ???????.

nginx, ?? ?? ?? ? ?? ??? ?? ? ? nginx, ?? ?? ?? ? ?? ??? ?? ? ? May 16, 2025 pm 10:54 PM

Nginx? ?? ?? ??? ?? ??? ???? ?? ? ??? ????? ???? ? ???? ??? ?? ?????. 1) ?? ?? ??? ????? /etc/nginx/nginx.conf???. ??? nginx-t ??? ???? ??? ??? ? ? ????. 2) ?? ???? ?? ?? (? : ???, Worker_Processes) ? HTTP ?? (? : log_format)? ?????. ??? ??? ?? ??? ?? ??? ?? ? ??? ?????. ??? ???? ?? ?? ??? ?? ???? ??? ? ????.

Linux? ??? ???? ???? ??? ?????? Ulimit? ???? ??? Linux? ??? ???? ???? ??? ?????? Ulimit? ???? ??? May 29, 2025 pm 11:09 PM

Linux System? Ulimit ??? ?? ??? ???? ???? ??? ??? ??? ?????. 1. ulimit? ?? ??? ? (-n), ??? ?? (-v), ??? ??? (-u) ?? ?? ? ??? ?? ? ? ?????. 2. ULIMIT-N2048? ?? ?? ??? ?? ULIMIT ??? ?? ????? ?? ???? ?????. 3. ??? ? ??? ???? /etc/security/limits.conf ? pam ?? ??? ???? sessionRequiredPam_limits.so? ???????. 4. SystemD ???? ?? ???? lim? ???????.

Debian Nginx ?? ??? ?????? Debian Nginx ?? ??? ?????? May 29, 2025 pm 11:06 PM

Debian ????? Nginx? ?? ? ? ??? ???? ????. ?? ??? ?? ?? ??? ?? : ??? ??? ? ? ?? ???? ??? ?? ?? NGINX ???? ??? ??? ?? ?? ??? ??????. ??? ?? ?? : NGINX? ???? ??? ???? ??? ???? ?? ?? ????? ?? ?????. HTTP ??? ?? : HTTP ???? ??? ?? ??? ???? ??? ?? ?? ? ?? ??? ???? ? ????. ?? ?? ?? worker_connections : ? ??? ???? ?? ? ??? ?? ?? ?? ???? ????? 1024? ?????. Multi_accept : ?? ?? ?? ??? ????? ?? ?? ??? ??????. ??

Debian Apache2? SEO ??? ??? ?????? Debian Apache2? SEO ??? ??? ?????? May 28, 2025 pm 05:03 PM

Debianapache2? SEO ??? ??? ?? ??? ????. ??? ??? : Keyword Magic Tools? ?? ?? (? : ??? ?? ??)? ???? ???? ?? ? ?? ???? ?????. ??? ??? ?? : ???? ??? ? ???? ????, ???? ??? ??? ??? ???? ?? ??? ? ??? ???????. ??? ???? ? ?? ??? : ??? ??? ???? ??? ?????. ???? ??? ??? ??? ??????. ??? ???? ?? ??? ??????. ?? ? ???? ?? ?????? ???? ??? ??????. ? ???? ???? ???? ??????. ?? ?? SEO ?? : robots.txt ?? : ?? ?? ???? ??? ??? ?????. ? ????? ??? : ?? ???? ? Apache ??? ???? ???

PHPStorm ? Docker Containerized ?? ??? ?????? PHPStorm ? Docker Containerized ?? ??? ?????? May 20, 2025 pm 07:54 PM

Docker Containization ??? ?? PHP ???? PHPStorm? ???? ?? ???? ?? ???? ???? ? ????. ?? ??? ??? ????. 1. PHP ??? ???? ?? Dockerfile? ????. 2. phpstorm?? Docker ??? ?????. 3. ???? ???? ?? dockercompose ??? ????. 4. ?? PHP ???? ??????. ??? ??? ?? ?????, ???? ? ?? ??? ??? ???? ?????.

Nginx ???? ?? ??? ???? ?? ?? Nginx ???? ?? ??? ???? ?? ?? May 16, 2025 pm 10:39 PM

nginx ??? ???? ??? ??? ????. 1. Systemd ??? ?? ?? : Sudonano/etc/systemd/system/nginx.service ? ?? ??? ?????. 2. SystemD ??? ???? : sudosystemctldaemon-reload. 3. nginx? ???? ?? ? ? ??? ??? : sudosystemctlenablenginx. ??? ??? ?? NGINX? ???? ???? ???? ????? ? ??? ?? ?? ????? ??? ? ??? ??? ?????.

????? Docker? ?? ??? ???? ?? ????? Docker? ?? ??? ???? ?? May 28, 2025 pm 04:33 PM

??? ????? Docker? ?? ?? ??? ??? ???? ?? ? ? ????. ??? ?? ???? ??? ????. 1 Docker ?? ??, Debian ???? ?? ??? ??????. Sudoaptupdatesudoaptupgrade-y ??? ??? ????? ???? ???? HTTPS? ?? ???? ?? ??? ???? ????? : Sudoaptinstallapt-Transportwortwartware-Common-Y Import gpg Key? ?? GPG ? : CULL.

See all articles