Sukiey / webapp-template

基于koa2,umijs,antd-pro5 的 web 应用
1 stars 0 forks source link

koa-router 使用通配符报错 #2

Open Sukiey opened 4 years ago

Sukiey commented 4 years ago

import as Router from 'koa-router'; const router = new Router(); router.all('', HomeCtrl.index); // 报错

Sukiey commented 4 years ago

【已解决】koa-router 路由通配符的写法 * 不正确,而应该写成 (*)

515195130 commented 4 years ago

改成(*)如:router.get("(*)") 依然报错,只有改成/(.*)报错问题才解决 如:router.get("/(.*)") 亲测有效!!

515195130 commented 4 years ago

// 返回404路由 这个路由必须写在最后面 router.get("/(.*)", async ctx => { await ctx.render("404", { title: "404" }); });

longbojun commented 3 years ago

router.all("",async (ctx,next)=>{ if (!ctx.session.user) { ctx.response.redirect('/users/login'); } else { await next(); } }); 同楼主一样报错了,该怎么改写?router.get("/(.)",回调)吗?

515195130 commented 3 years ago

router.get("/(.*)", async ctx => {   ctx.body = "<h1>404页面</h1>" });

------------------ 原始邮件 ------------------ 发件人: "Sukiey/webapp-template" @.>; 发送时间: 2021年6月1日(星期二) 晚上11:06 @.>; @.**@.>; 主题: Re: [Sukiey/webapp-template] koa-router 使用通配符报错 (#2)

router.all("",async (ctx,next)=>{ if (!ctx.session.user) { ctx.response.redirect('/users/login'); } else { await next(); } }); 同楼主一样报错了,该怎么改写?router.get("/(.)",回调)吗?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

iceycc commented 2 years ago

aaronchen233 commented 1 year ago

// 防止vue路由history模式导致404 router.get(/(.*)/, async (ctx, next) => { ctx.type = 'html' ctx.body = fs.createReadStream( path.resolve(__dirname, '../public/index.html') ) })