Home Web Front-end JS Tutorial How to set up a reverse proxy using webpack

How to set up a reverse proxy using webpack

Jun 07, 2018 pm 02:44 PM
server webpack reverse proxy Cross domain

This time I will show you how to use webpack to set up a reverse proxy, and what are the precautions for using webpack to set up a reverse proxy. The following is a practical case, let's take a look.

1. Reasons for setting up a proxy

Now the requirements for front-end development are getting higher and higher, and with the birth of automation and modularization , the front-end and back-end development model is becoming more and more popular. The backend is only responsible for the interface, and the frontend is responsible for data display and logical processing. However, there is an important issue in the front-end and back-end development model, which is the cross-domain issue.

2. How to configure the webpack agent

The webpack agent requires another plug-in: webpack-dev-server

webpack -dev-server is very convenient to configure the proxy. You only need to condition a proxy attribute and then configure the relevant parameters:

var webpack = require('webpack');
var WebpackDevServer = require("webpack-dev-server");
var path = require('path');
var CURRENT_PATH = path.resolve(__dirname); // 获取到当前目录
var ROOT_PATH = path.join(__dirname, '../'); // 项目根目录
var MODULES_PATH = path.join(ROOT_PATH, './node_modules'); // node包目录
var BUILD_PATH = path.join(ROOT_PATH, './dist'); // 最后输出放置公共资源的目录
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
  //项目的文件夹 可以直接用文件夹名称 默认会找index.js ,也可以确定是哪个文件名字
  entry: {
    app: ['./src/js/index.js'],
    vendors: ['jquery', 'moment'], //需要打包的第三方插件
    // login:['./src/css/login.less']
  },
  //输出的文件名,合并以后的js会命名为bundle.js
  output: {
    path: path.join(__dirname, "dist/"),
    publicPath: "http://localhost:8088/dist/",
    filename: "bundle_[name].js"
  },
  devServer: {
    historyApiFallback: true,
    contentBase: "./",
    quiet: false, //控制台中不输出打包的信息
    noInfo: false,
    hot: true, //开启热点
    inline: true, //开启页面自动刷新
    lazy: false, //不启动懒加载
    progress: true, //显示打包的进度
    watchOptions: {
      aggregateTimeout: 300
    },
    port: '8088', //设置端口号
    //其实很简单的,只要配置这个参数就可以了
    proxy: {
      '/index.php': {
        target: 'http://localhost:80/index.php',
        secure: false
      }
    }
  } 
..........
};
Copy after login

In the above example, we set the local port number to: 8088. If at this time The interface is placed on the server with port 80, and the interface URL we request is as follows: http://localhost:80/index.php

At this time, we only need to use regular expression matching /index in the proxy. php, and then match the target interface set by the redirect target; when using the ajax request interface, do not write the domain name of the target interface, just write index.php.

 $.ajax({
    type: 'GET',
    url: '/index.php',
    data: {},
    dataType: 'json',
    beforeSend: function () {
    },
    success: function (data) {
    },
    error: function (error) {
    }
  });
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to sort json objects and delete data with the same id

How to perform mvvm in actual projects -simple two-way binding

The above is the detailed content of How to set up a reverse proxy using webpack. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Nginx with FastAPI for reverse proxy and load balancing How to use Nginx with FastAPI for reverse proxy and load balancing Aug 01, 2023 am 09:44 AM

How to use Nginx with FastAPI for reverse proxy and load balancing

Solution to PHP Session cross-domain problem Solution to PHP Session cross-domain problem Oct 12, 2023 pm 03:00 PM

Solution to PHP Session cross-domain problem

Detailed explanation of reverse proxy and request forwarding in Gin framework Detailed explanation of reverse proxy and request forwarding in Gin framework Jun 23, 2023 am 11:43 AM

Detailed explanation of reverse proxy and request forwarding in Gin framework

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

How to install, uninstall, and reset Windows server backup

How to use Nginx Proxy Manager to implement reverse proxy under HTTPS protocol How to use Nginx Proxy Manager to implement reverse proxy under HTTPS protocol Sep 26, 2023 am 08:40 AM

How to use Nginx Proxy Manager to implement reverse proxy under HTTPS protocol

Nginx reverse proxy cache configuration to improve website access speed Nginx reverse proxy cache configuration to improve website access speed Jul 04, 2023 pm 10:01 PM

Nginx reverse proxy cache configuration to improve website access speed

Nginx reverse proxy cache configuration to accelerate static web page access Nginx reverse proxy cache configuration to accelerate static web page access Jul 04, 2023 pm 06:09 PM

Nginx reverse proxy cache configuration to accelerate static web page access

How to implement the reverse proxy function in the Workerman document How to implement the reverse proxy function in the Workerman document Nov 08, 2023 pm 03:46 PM

How to implement the reverse proxy function in the Workerman document

See all articles