Luk*_*und 5 html javascript ejs node.js express
我想让我的网页模块化,这样我就不需要一次又一次地写我的东西。我以为我的解决方案是 ejs lib。所以我使用了配置如下的 express 和 ejs:
const app = express();
app.engine('.html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/wwwroot/views');
Run Code Online (Sandbox Code Playgroud)
我的视图文件夹结构如下所示:
wwwroot
static
css
js
views
login
index.html
profile
dashboard.html
templates
inc_header.html
inc_footer.html
Run Code Online (Sandbox Code Playgroud)
我的仪表盘有以下内容
<% include templates/inc_header.html %>
This is my dashboard
Run Code Online (Sandbox Code Playgroud)
头文件将不包括在内。我试过 wwwroot/views/templates/header.html 和 header.html。什么都行不通。
app.engine('.html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/wwwroot/views');
app.use(session({
secret: program.secret || "secret",
resave: true,
saveUninitialized: true
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))
app.use('/', express.static(path.join(__dirname, '/wwwroot/static/')));
Run Code Online (Sandbox Code Playgroud)
<% include templates/inc_header.html %> 这是我的仪表板
看起来文件不会被渲染?
这是来自 ejs文档:
包含与包含调用的模板相关。
由于包含标头的文件是,dashboard.html那么路径应该是:
<% include ../templates/inc_header %>
Run Code Online (Sandbox Code Playgroud)