因爲系統業務比較複雜,微前端(多頁應用)實現指望用一個app一個webpack的形式,這樣能夠更好隔離,並且每一個app的webpack config能夠定製。html
Nginx在前端項目中安裝比較麻煩,因此考慮用nodejs來代理,社區比較成熟的node代理顯然就是express的http-proxy-middleware了:前端
var express = require('express') var proxy = require('http-proxy-middleware') var rewrite = require('express-urlrewrite') var app = express() let options = { '/report/template': { target: 'http://www.wenming.cn', pathRewrite: { '^/report/template': '', }, headers: { "Host": "www.wenming.cn", "Connection": "keep-alive", "Cache-Control": "max-age=0", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.9", "Cookie": "wdcid=118bf0416992ec45; wdlast=1553783233; Hm_lvt_3bdce7455c04dfb8cf1a26c3ac808bea=1553783233; Hm_lpvt_3bdce7455c04dfb8cf1a26c3ac808bea=1553783233", } }, '/report':{ target: 'http://localhost:8090', pathRewrite: { '^/report': '', }, } } app.use('/app/home', (req,res,next) => { req.originalUrl = req.originalUrl.replace(/^\/app\/home/, '') next() }); app.use(rewrite('/app/home/*', '/$1')) function proxyFactory(source, option) { app.use(source, proxy(option)) } for (let key in options) { proxyFactory(key, options[key]) } app.use('/', (req, res) => { res.send('not found!!') }) app.listen(3000)
webpack-dev-server同時起多個:node
const execa = require('execa') execa('webpack-dev-server', ['--entry', './apps/alarm.js', '--port', '8044', '--config', './webpack.js'], {stdio: 'inherit'}) execa('webpack-dev-server', ['--entry', './apps/report.js', '--port', '8045', '--config', './webpack.js'], {stdio: 'inherit'})