运行环境os ~ ubuntu koa ~ 2.3.0 koa-router ~ 7.2.1 koa-views 6.0.2,ejs ~ 2.5.7
项目主要结构
部分路由设置
router.get('/users/login',async (ctx,next)=>{
`await ctx.render('login')`
})
app.use(views(__dirname + '/views',{
`map:{html:'ejs'}`
}));
app.use(koa_static(__dirname + '/public'));
最近研究node ,遇到问题,当使用'/users/login'路径时,会遇到ejs模板中的静态文件路径发生错误的情况,
例如http://localhost:3000/users/bower_components/responsive-tables/responsive-tables.js
但是使用'/login'路径时静态文件都是可以加载出来的
正确的情况
错误的情况
http://localhost:3000/bower_components/responsive-tables/responsive-tables.js
这个才是正确的路径,请问,在使用多层嵌套路由时,如何在模板页面正确加载出静态文件
你使用static中间件,所有html里面用到的静态资源都会去你设置的静态资源目录里查找,
没搞懂你这里说的多层嵌套路由是什么,
反正把静态资源放到public里去,然后html里通过相对于public的路径使用,应该可以解决你的问题