The front-end solves the proxy server, which is generally the work of the back-end.

In fact, the proxy server is the task of the back-end, solving cross-domain or something, but maybe the back-end is too busy to solve it, then we can solve the front-end first.
Download the instruction npm i express cors axios to
name a js file

const express = require("express");
const cors = require("cors"); // 是用来解决跨域的
const axios = require("axios").default; // 发送网络请求
const app = express();

// req request  请求
// res response 相应
// app.get("/", (req, res) => {
//   res.json({
//     code: 1,
//     msg: "",
//   });
// });

app.use(express.urlencoded()); // 可以解析url编码的数据
app.use(express.json()); // 可以解析json编码的数据
app.use(cors()); // 解决跨域

// 使用自己写的方法实现代理请求第三方网站的接口
app.post("/api/v1/proxy", (req, res) => {
  const { url } = req.body;
  // 参数url表示我需要请求的地址
  axios.get(url).then((result) => {
    res.json(result.data);
  });
  // console.log(req.body); // 请求体中的数据
  // res.json({
  //   code: 2,
  //   msg: "获取到数据了",
  // });
});

app.listen(3003, () => {
  console.log("服务器运行在3003端口");
});

To configure a proxy server across domains, see this https://blog.csdn.net/lzfengquan/article/details/109063265?spm=1001.2014.3001.5501

Guess you like

Origin blog.csdn.net/lzfengquan/article/details/122978759