当前位置 : 主页 > 网页制作 > html >

webpack 4 的 proxy

来源:互联网 收集:自由互联 发布时间:2021-06-12
第一种 接口有api的 setupProxy.js const proxy = require(‘http-proxy-middleware‘ );module.exports = function (app) { app.use(proxy( ‘/api‘ , { target: ‘ http://127.0.0.1:8081 ‘ , secure: false , changeOrigin: true , pathRew

第一种 接口有api的

setupProxy.js

const proxy = require(‘http-proxy-middleware‘);

module.exports = function(app) {
  app.use(proxy(‘/api‘, { 
       target: http://127.0.0.1:8081,
       secure: false,
       changeOrigin: true,
       pathRewrite: {
        "^/api": "/api"
       },
    }));
};

 

react的请求api

import ly_req from ‘../config/ly_req‘
// const common=‘api/mock/5d50cfdbc7a4ef40e1081602/sy‘;
const common=‘/api;
// export const sy_userIndex =  data  => ly_req(`/${common}/v1/userBaseinfoList`,data,‘GET‘);
export const sy_userIndex =  data  => ly_req(`${common}/user/id/9`,data,‘GET‘);

yml

server:
  port: 8081 #项目端口
  servlet:
    context-path: /

Controller

@RestController
@RequestMapping("api/user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping("/id/{userId}")
    public User testselectById(@PathVariable Integer userId) {

第二种 接口没有api的

setupProxy.js

const proxy = require(‘http-proxy-middleware‘);

module.exports = function(app) {
  app.use(proxy(‘/api‘, { 
       target: ‘http://127.0.0.1:8081‘,
       secure: false,
       changeOrigin: true,
       pathRewrite: {
        "^/api": "/"
       },
    }));
};

react的请求api

import ly_req from ‘../config/ly_req‘
// const common=‘api/mock/5d50cfdbc7a4ef40e1081602/sy‘;
const common=‘/api‘;
// export const sy_userIndex =  data  => ly_req(`/${common}/v1/userBaseinfoList`,data,‘GET‘);
export const sy_userIndex =  data  => ly_req(`${common}/user/id/9`,data,‘GET‘);

yml  同上

Controller

@RestController
@RequestMapping("user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping("/id/{userId}")
    public User testselectById(@PathVariable Integer userId) {
网友评论