当前位置:网站首页>创建一个自己的跨域代理服务器
创建一个自己的跨域代理服务器
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
然后就可以按照上面的方式,代理你想代理的服务了
边栏推荐
- Processing random generation line animation
- 装饰器(Decorator)
- POJ 3070 Fibonacci
- 计算两点之间的距离(二维、三维)
- Oplg: new generation cloud native observable best practices
- SQL assigns the field value of data table B to a column in data table a
- Electron official docs series: References
- 三维向量的夹角
- C language: Exercise 2
- Generate JDE dot train
猜你喜欢

ES中索引别名(alias)的到底有什么用

MariaDB study notes

MySQL数据库常见故障——遗忘数据库密码
![8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)](/img/e7/2fd8ec8d5e5473c7f84f3e3bcedca8.png)
8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)

Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units

Echart stack histogram: add white spacing effect setting between color blocks

awk工具

Here Document免交互及Expect自动化交互

Beifu PLC obtains system time, local time, current time zone and system time zone conversion through program

8、【STM32】定时器(TIM)——中断、PWM、输入捕获实验(一文精通定时器)
随机推荐
I - Dollar Dayz
MySQL讲解(二)
Dark horse notes - Common APIs
Mysql database explanation (IV)
7-16 monetary system I
Script - crawl the customized storage path of the cartoon and download it to the local
Mysql database explanation (III)
What features are added to Photoshop 2022 23.4.1? Do you know anything
橋接模式(Bridge)
Luogu p3426 [poi2005]sza-template solution
Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏
ES基於Snapshot(快照)的數據備份和還原
Coprime and non coprime of template problems of Chinese remainder theorem
Appearance mode (facade)
Uva11582 [fast power]colossal Fibonacci numbers!
8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)
Electron official docs series: Development
Here Document免交互及Expect自动化交互
外观模式(Facade)
Solutions to insufficient display permissions of find and Du -sh