Node.js PugJs-找不到模板文件/忽略目录
我有一个简单的应用程序,可以输出一小部分内容。我使用NodeJs、Express和PugNode.js PugJs-找不到模板文件/忽略目录,node.js,pug,pugjs,Node.js,Pug,Pugjs,我有一个简单的应用程序,可以输出一小部分内容。我使用NodeJs、Express和Pug const pug = require('pug'); const express = require('express'); const app = express(); const indexFile = 'index'; // the file to load app.set('view engine', 'pug'); // use pug app.get('/', function (req
const pug = require('pug');
const express = require('express');
const app = express();
const indexFile = 'index'; // the file to load
app.set('view engine', 'pug'); // use pug
app.get('/', function (req, res) {
res.render(indexFile, { templateToLoad: 'Views/Templates/temp.pug' }); // include a template file
});
app.listen(8888, function () {
console.log('Server running on port 8888');
});
还有我的哈巴狗文件/HTML
doctype html
html
body
p default content on all pages
include templateToLoad
我的目录看起来像这样
启动服务器时,我收到此错误消息
Error: ENOENT: no such file or directory, open 'C:\Users\mah\Desktop\Test\views\templateToLoad.pug'
at C:\Users\mah\Desktop\Test\views\index.pug line 5
at Object.fs.openSync (fs.js:584:18)
at Object.fs.readFileSync (fs.js:491:33)
at Function.read (C:\Users\mah\Desktop\Test\node_modules\pug-load\index.js:69:13)
at Object.read (C:\Users\mah\Desktop\Test\node_modules\pug\lib\index.js:147:25)
at C:\Users\mah\Desktop\Test\node_modules\pug-load\index.js:24:25
at walkAST (C:\Users\mah\Desktop\Test\node_modules\pug-walk\index.js:23:18)
at C:\Users\mah\Desktop\Test\node_modules\pug-walk\index.js:104:20
at Array.reduce (native)
at walkAndMergeNodes (C:\Users\mah\Desktop\Test\node_modules\pug-walk\index.js:103:18)
at walkAST (C:\Users\mah\Desktop\Test\node_modules\pug-walk\index.js:37:19)
但是当调试
templateToLoad
时,我得到了'Views/Templates/temp.pug'
。为什么文件夹Templates
被忽略?做了一些深入研究-似乎帕格不支持动态包含(您正在尝试这样做)
可能的解决方法包括使用jade.render
或重新使用res.render
本身来生成模板
- 动态包含在pug中。
我建议您发送模板名称并在index.pug中使用,以显示所需的模板
注:
temp1
,temp2
tempN
需要存在于Templates
文件夹中
doctype html
html
body
p default content on all pages
if templateToLoad == "temp1"
include Templates/temp
else if templateToLoad == "temp2"
include Templates/temp2
发送模板名
app.get('/', function (req, res) {
res.render(indexFile, { templateToLoad: 'temp1' }); // include a template file
});
帕格提出模板继承 因此,不渲染index.pug,而是渲染模板: index.pug:
doctype html
html
body
p default content on all pages
block content
临时帕格犬:
extends index.pug
block content
p My template
你的server.js
app.get('/', function (req, res) {
res.render('temp');
});
尝试将
视图/Templates/temp.pug
更改为/View/Templates/temp.pug
不抱歉,这是相同的错误消息抱歉它似乎与索引.pug
相关-尝试/Templates/temp.pug
(或/Templates/temp.pug
)嗯,不抱歉,相同的错误消息:/evenTemplates/temp.pug
不起作用可能我必须使用path
模块?因此这几乎与我的代码相似,在这里不可能编写类似的内容Templates/+templateToLoad