webpack 4 的 proxy

第一种 接口有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) {

 

posted @ 2019-08-22 11:24  ThisCall  阅读(922)  评论(0编辑  收藏  举报