webpack-12-跨域处理

webpack处理跨域

  1. 代理,重定向

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    devServer: {
    proxy: {
    '/api': { // 捕获路径
    target: 'http://localhost:3000', // 重定向
    pathRewrite: {
    api: '' // 重写path
    }
    }
    }
    }
  2. 前端mock数据模拟

    1
    2
    3
    4
    5
    6
    7
    devServer: { 
    before(app) {
    app.get('/api/user', (req, res) => {
    res.end('hello')
    })
    }
    }
  3. 服务端运行

    webpack-dev-middleware

    1
    2
    3
    4
    5
    6
    const WebpackDevMiddleware = require('webpack-dev-middleware')

    const config = require('./webpack.config')
    const compiler = webpack(config)

    app.use(WebpackDevMiddleware(compiler))
0%