开门见山
本篇文章主要是介绍使用koa上线Vue项目,希望可以帮助到大家
安装依赖
cnpm i koa koa-static http-proxy-middleware koa2-connect
代码演示
const path = require('path')
const Koa = require('koa')
const static = require('koa-static')
const { createProxyMiddleware } = require('http-proxy-middleware');
const koaConnect = require('koa2-connect')
const app = new Koa()
// 引入静态文件
app.use(static(path.join(__dirname, '../dist')))
// 代理兼容封装
const proxy = function (context, options) {
if (typeof options === 'string') {
options = {
target: options
}
}
return async function (ctx, next) {
await koaConnect(createProxyMiddleware(context, options))(ctx, next)
}
}
// 代理配置
const proxyTable = {
'/boss': {
target: 'http://eduboss.lagou.com',
changeOrigin: true
},
'/front': {
target: 'http://edufront.lagou.com',
changeOrigin: true
}
}
Object.keys(proxyTable).map(context => {
const options = proxyTable[context]
// 使用代理
app.use(proxy(context, options))
})
app.listen(3001, () => console.log('3001'))
谢谢观看,如有不足,敬请指教