相对路由是相对于哪里的路由?

2020-04-15  本文已影响0人  eftales

问题演示

当前有两个路由:

'GET /tanda/:productKey': async (ctx, next) =>{
  ctx.render('tanda.html');
},
'POST /tanda/': async (ctx, next) =>{
  ctx.render('tanda.html');
}

tanda.html 里面都有一个调用静态资源的 link

<link href="static/css/style.css" rel="stylesheet" type="text/css">

第一个路由请求的网址实际是:

tanda/static/css/style.css

第二个路由请求的网址实际是:

static/css/style.css

所以只有第二个可以访问到静态资源。

原因

/ 、 /tanda 、/static 同级,但是 /tanda/:productKey 是 / 的下级。

解决方案

静态资源最好写成绝对路径:

<link href="/static/css/style.css" rel="stylesheet" type="text/css">
上一篇 下一篇

猜你喜欢

热点阅读