background

Since HTTP-proxy does not support the pathRewrite parameter, there is no other option but to use the HTTP-proxy-Middleware plugin.

The project depends on

  • connect
  • serve-static
  • http-proxy-middleware
  • http
  • open

Refer to rely on

const connect = require('connect');
const serveStatic = require('serve-static');
const http = require('http');
const {createProxyMiddleware} = require('http-proxy-middleware');
Copy the code

The basic configuration

Const config = {target: 'http://xx.xx.xx.xx', port: 3001, host: '0.0.0.0', dir: '.. /dist', prefix: '/api', publicPath: '/' };Copy the code

The HTTP proxy – middleware configuration

const proxyConfigMiddleware = {
    target: config.target,
    changeOrigin: true,
    logLevel: 'debug',
    // path rewrite 
    pathRewrite: {
        ['^' + config.prefix]: ''
    }
};
Copy the code

CreateProxyMiddleware using the createProxyMiddleware method

const proxyMiddleware = createProxyMiddleware(proxyConfigMiddleware);

Copy the code

Configure the local HTTP server

app.use(config.prefix, proxyMiddleware);
app.use(
    config.publicPath,
    serveStatic(config.dir, {
        index: ['index.html', '/']
    })
);

http.createServer(app).listen(config.port, config.host);
Copy the code

Call open to open the default browser

require('open')('http://localhost:' + config.port);
Copy the code

The project address

Github.com/bosscheng/d…