node.js - 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, res) {
  res.render(indexFile, { templateToLoad: 'Views/Templates/temp.pug' }); // include a template file
});

app.listen(8888, function () {
  console.log('Server running on port 8888');
});

还有我的 Pug 文件/HTML

doctype html
html
    body
      p default content on all pages
      include templateToLoad

我的目录是这样的

dir

启动服务器时我收到此错误消息

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 被忽略?

最佳答案

动态包含是 not supported在帕格。 我建议您发送模板名称并使用 conditionals在您的 index.pug 中显示所需的模板。

注意:temp1temp2 ... 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
});

关于node.js - PugJs - 找不到模板文件/忽略目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47408127/

相关文章:

javascript - Next.js API 路由中的正文超出 1mb 限制错误

javascript - 如何控制台登录哈巴狗?

node.js - 错误 : Cannot find module 'pug'

javascript - 用 js 发送原始比特币交易?

c++ - Node Js 与 C++ 简单数学性能对比

javascript - aws-serverless-express 具有 serverless-offline 阶段中断路由

html - 如何在 Jade 中更改页面的背景颜色?

javascript - 错误: Failed to lookup view -> simple code

variables - jade mixin 中的动态元素

intellij-idea - Intellij IDEA 中对 Pug 模板引擎的支持