快递和EJS-未使用layout.ejs [英] Express & EJS - layout.ejs not used

查看:45
本文介绍了快递和EJS-未使用layout.ejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Express将EJS作为视图引擎.似乎未使用我的layout.ejs.我的视图"文件夹中有两个视图index.ejs和layout.ejs.似乎已呈现index.js,但未呈现layout.ejs.layout.ejs文件应包含CSS文件,但是在浏览器中呈现页面时,该文件不存在.我放置在layout.ejs文件中的任何测试测试文本都不会与响应一起输出.

Im trying out EJS as a view engine with Express. It seems that my layout.ejs is not used. I have two views index.ejs and layout.ejs both in my 'views' folder. It seems that index.js is rendered but layout.ejs is not. The layout.ejs file should be including a CSS file but when the page is rendered in the browser this is not there. Any test test text that I place in the layout.ejs file is not output with the response.

我是否缺少其他配置步骤?

Am I missing an additional configuration step?

谢谢!

我的server.js代码:

My server.js code:

var express = require('express');

var app = express();
    app.set('view engine', 'ejs');
    app.use(express.static(__dirname + '/public'));
    app.get('/', function(req, res){
            res.render('index.ejs', {title: 'EJS Engine'});
    });

    app.listen(8080);

在我的layout.ejs中,我链接到位于公共文件夹中的单个css文件.

In my layout.ejs I am linking to a single css file which resides in the public folder.

layout.ejs:

layout.ejs:

<!DOCTYPE html>
       <html>
       <head>
           <title><%= title %></title>
            <link rel="stylesheet" type="text/css" href="main.css">
       </head>
       <body>
              <%- body %>
       </body>
       </html> 

index.ejs

index.ejs

<div id="container">
        index.html
</div>

推荐答案

这是您需要的模块: https://www.npmjs.org/package/express-ejs-layouts

执行以下操作:

npm install express-ejs-layouts // install the layout module from the command line

var express = require("express")
    ,path = require('path')
    ,fs = require('fs')
    ,expressLayouts=require("express-ejs-layouts") // add this requirement
    , app = express();

app.use(express.bodyParser());
app.use(expressLayouts); // add this use()
app.use(express.static(__dirname));

现在ejs引擎应该使用您的布局.

Now the ejs engine should use your layout.

这篇关于快递和EJS-未使用layout.ejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆