koa2 vue单页面 history 模式怎么配置?

发布于 2022-09-11 17:07:08 字数 941 浏览 31 评论 0

koa2在配置vue单页面history 模式时,使用了 koa2-connect-history-api-fallback,按文档写了没有效果,请路过者指点一二,不胜感激!
部分代码如下:

const Koa = require('koa')
const app = new Koa()
const views = require('koa-views')
const json = require('koa-json')
const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')
const history = require('./middleware/koa2-connect-history-api-fallback');

app.use(history())
middlewares
app.use(bodyparser({
    enableTypes: ['json', 'form', 'text']
}))
app.use(json())
app.use(logger())
app.use(require('koa-static')(__dirname + '/dist'))




// logger
app.use(async (ctx, next) => {
    const start = new Date()
    await next()
    const ms = new Date() - start
    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})


// error-handling
app.on('error', (err, ctx) => {
    console.error('server error', err, ctx)
});

module.exports = app

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必你懂 2022-09-18 17:07:08

你得指定你的入口html路径

述情 2022-09-18 17:07:08

在根目录下添加middleware文件夹,添加koa2-connect-history-api-fallback.js 文件了吗?

半世蒼涼 2022-09-18 17:07:08

Github地址:https://github.com/ishen7/koa...

下面是例子

const Koa \= require('koa');

// require 'koa2-connect-history-api-fallback' middleware
const { historyApiFallback } \= require('koa2-connect-history-api-fallback');

// create app
const app \= new Koa();

// use historyApiFallback
app.use(historyApiFallback());

// other middlewares
app.use(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文