修改锚标记,href的URL时无法呈现Ejs文件

时间:2018-11-17 18:13:26

标签: node.js express ejs

<a class="active" href="/dashboard">
   <i class="fa fa-dashboard">
  </i><span>Dashboard</span>
  </a>

router.get('/dashboard',function(req,res,next){
   res.render('dashboard');
});

在上面的这一部分中,“ dashboard.ejs”文件将按预期方式呈现,但是当我如下图所示对其进行修改时,“ dashboard.ejs”文件将无法呈现!

 <a class="active" href="/dashboard/dashboardcontent">
   <i class="fa fa-dashboard">
  </i><span>Dashboard</span>
  </a>

router.get('/dashboard/dashboardcontent',function(req,res,next){
   res.render('dashboard');
});

我想知道为什么在进行了如此小的更改后它仍无法呈现文件。

有帮助吗?

1 个答案:

答案 0 :(得分:0)

在找到解决方案之前:

app.use(express.static(path.join(__dirname, 'public')));

我们可以看到,我没有为express.static函数提供的文件创建虚拟路径前缀!

解决方案:

要为express.static函数提供的文件创建虚拟路径前缀(该路径在文件系统中实际上不存在),请为静态目录指定安装路径,如下所示:

app.use('/static', express.static(path.join(__dirname, 'public')));

我还在“ layout.ejs”文件中的所有CSS,JS,图像链接中添加了“ / static”,如下所示:

<!-- Bootstrap core CSS -->
<link href="/static/stylesheets/bootstrap.css" rel="stylesheet">
<!--external css-->
<link href="/static/font-awesome/css/font-awesome.css" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="/static/stylesheets/zabuto_calendar.css">
<link rel="stylesheet" type="text/css" href="/static/javascripts/gritter/css/jquery.gritter.css" />
<link rel="stylesheet" type="text/css" href="/static/lineicons/style.css">    

<!-- Custom styles for this template -->
<link href="/static/stylesheets/style.css" rel="stylesheet">
<link href="/static/stylesheets/style-responsive.css" rel="stylesheet">
<script src="/static/lib/jquery.min.js" type="text/javascript"></script>
<script src="/static/dist/jquery.validate.min.js" type="text/javascript"></script>
<script src="/static/javascripts/chart-master/Chart.js"></script>
<script src="/static/select2Plugin/select2.min.js"></script>
<link rel="stylesheet" href="/static/select2Plugin/select2.min.css" />

来源:https://expressjs.com/en/starter/static-files.html

       Hope this will be found helpful.