当前位置:网站首页>创建一个自己的跨域代理服务器
创建一个自己的跨域代理服务器
2022-06-26 12:41:00 【南风number】
项目无需重启,就可以实现跨域代理
新建一个文件夹 lightweight-proxy
- 1.在该文件夹下,使用cmd打开
依次执行一下命令,进行初始化
yarn init
git init
yarn add express
- 2.安装 http-proxy-middleware 中间件
yarn add http-proxy-middleware
在 package.json 中的 scripts 下新建一个名为 dev 的脚本,内容为:node src/index.mjs
- 3.在根目录下新建 src/index.mjs,并初始化内容:
import express from 'express'
import {
createProxyMiddleware } from 'http-proxy-middleware';
const app = express();
// --------- 看这里 ↓ -------------------------
app.use('/', createProxyMiddleware({
target: 'https://juejin.cn/',
changeOrigin: true
}));
// --------- 看这里 ↑ -------------------------
app.listen(3000);
以上代码的关键就在注释中着重强调的区域,为路由 ‘/’ 下的所有请求指定使用一个中间件,代理到 ‘https://juejin.cn/’ 地址上去。
因此,当你在浏览器中输入 http://localhost:3000 时,你会惊奇地发现,你打开了掘金的主页:(虽然,因为掘金官网 “防盗链” 机制的存在,你无法刷出文章列表。)
实际项目中的代理
import express from 'express'
import {
createProxyMiddleware } from 'http-proxy-middleware';
const app = express();
// --------- 看这里 ↓ -------------------------
// 凡是以 '/prod-api' 开头的都认定为 xhr 请求
app.use('/prod-api', createProxyMiddleware({
target: 'https://dev.my-server.com/', // 这一行是你的服务端地址
changeOrigin: true,
pathRewrite: {
'^/prod-api': '' // 在向服务端发起请求时,去掉标识xhr的前缀
}
}));
// 其他请求则认定为前端资源请求,如:html/js/css 等
app.use('/', createProxyMiddleware({
target: 'http://localhost:9527/',
changeOrigin: true
}));
// --------- 看这里 ↑ -------------------------
app.listen(3000);
启动代理服务
npm run dev
然后就可以按照上面的方式,代理你想代理的服务了
边栏推荐
- J - Wooden Sticks poj 1065
- C language: Exercise 2
- Analysis of state transition diagram of Beifu NC axis
- Mysql database explanation (IV)
- ES中索引别名(alias)的到底有什么用
- 2、并行接口、协议和相关芯片介绍(8080、8060)
- What should the software test report include? Interview must ask
- Stack, LIFO
- Custom encapsulation drop-down component
- Design of four kinds of linear phase FIR filters -- complete set of Matlab source code
猜你喜欢
MySQL explanation (I)
Awk tools
ES6 module
Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏
原型模式(prototype)
Fire warning is completed within 10 seconds, and Baidu AI Cloud helps Kunming Guandu build a new benchmark of smart city
利用scrapy爬取句子迷网站优美句子存储到本地(喜欢摘抄的人有福了!)
Beifu realizes the control of time slice size and quantity through CTU and ton
Beifu twincat3 can read and write CSV and txt files
ES基於Snapshot(快照)的數據備份和還原
随机推荐
J - Wooden Sticks poj 1065
A collection of common tools for making we media videos
Bigint: handles large numbers (integers of any length)
7-16 monetary system I
QT . Establishment and use of pri
F - Charm Bracelet
MySQL数据库讲解(四)
Solutions to insufficient display permissions of find and Du -sh
Digital signal processing -- Design of linear phase type (Ⅰ, Ⅲ) FIR filter (1)
Typescript
Here document interaction free and expect automatic interaction
Vivado error code [drc pdcn-2721] resolution
Design of simple digital circuit traffic light
ES6:Map
防火墙介绍
MediaPipe手势(Hands)
[how to connect the network] Chapter 1: the browser generates messages
Oplg: new generation cloud native observable best practices
MySQL explanation (I)
Electron official docs series: Distribution