每年培训输出1000+合格IT工程师
培训影响全国各大知名IT企业和部门

koa2-connect-history-api-fallback

const Koa = require("koa2")
const Static = require("koa-static")
const Router = require("koa-router")
const fs = require("fs")
const path = require("path")

let app = new Koa()

const {historyApiFallback} = require('koa2-connect-history-api-fallback');
//这句代码需要在koa-static上面
app.use(historyApiFallback({
    htmlAcceptHeaders: ['text/html', 'application/xhtml+xml']
}));

app.use(Static(path.resolve(__dirname,"..")))

let router = new Router()

router.get("/api",async(cxt)=>{
    cxt.body=  cxt.query
})

router.get("/",async(cxt)=>{
    cxt.type="text/html"
    cxt.body = fs.createReadStream(path.resolve(__dirname,"..","dist/index.html"))
})


app.use(router.routes()).use(router.allowedMethods())


app.listen(3000,()=>{
    console.log("server is ready on port 3000")
})
赞(1) 打赏
未经允许不得转载:徐礼文的技术博客 » koa2-connect-history-api-fallback
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏