Koa2 静态服务及代理配置
生活随笔
收集整理的這篇文章主要介紹了
Koa2 静态服务及代理配置
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
const path = require('path')
const Koa = require('koa')
const static = require('koa-static')
const httpProxyMiddleware = require('http-proxy-middleware')
const koaConnect = require('koa2-connect')const app = new Koa()// 引入靜態(tài)文件
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(httpProxyMiddleware(context, options))(ctx, next)}
}// 代理配置
const proxyTable = {'/api': {target: 'http://localhost:3333',changeOrigin: true}
}Object.keys(proxyTable).map(context => {const options = proxyTable[context]// 使用代理app.use(proxy(context, options))
})app.listen(3001, () => console.log('3001'))
github
轉(zhuǎn)載于:https://www.cnblogs.com/lshilin/p/10369759.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Koa2 静态服务及代理配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 版本号命名规范及原则
- 下一篇: 使用devcon禁用启用网卡