使用普通布局为哈巴狗的所有页面

时间:2017-04-25 02:22:42

标签: javascript node.js express pug

我正在用快递

尝试哈巴狗
views/layout.pug 

doctype html
html
  head
    title= title
    link(rel="shortcut icon", href="favicon.ico", type="image/x-icon")
    link(rel='stylesheet', href='/assets/application.css')
    link(rel='stylesheet', href="assets/libs/bootstrap/dist/css/bootstrap.min.css")
  body    
    script(src="assets/libs/jquery/dist/jquery.min.js")
    script(src='assets/libs/bootstrap/dist/js/bootstrap.min.js')
    script(src="assets/application.js")
    block content

这是用户页面的路由器。

routes/user.js

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.render('user/index');
});

module.exports = router;

我有用户/索引页面的视图。我从视图中扩展了布局。用户索引页面使用布局。 layout.pug存在于views文件夹中。

如果我在views / user中添加一个layout.pug就可以了。但是如何使用views / layout.pug

/views/user/index.pug

extends layout


block content
    h1
     | User index

我收到以下错误。它正在查看视图/用户文件夹中的布局。如何使用view / layout.pug。

Error: ENOENT: no such file or directory, open 'C:\my_projects\myexpressapp\views\user\layout.pug'
    at C:\my_projects\myexpressapp\views\user\index.pug line 1
    at Error (native)
    at Object.fs.openSync (fs.js:640:18)
    at Object.fs.readFileSync (fs.js:508:33)
    at Function.read (C:\my_projects\myexpressapp\node_modules\pug-load\index.js:69:13)
    at Object.read (C:\my_projects\myexpressapp\node_modules\pug\lib\index.js:147:25)
    at C:\my_projects\myexpressapp\node_modules\pug-load\index.js:24:25
    at walkAST (C:\my_projects\myexpressapp\node_modules\pug-walk\index.js:23:18)
    at C:\my_projects\myexpressapp\node_modules\pug-walk\index.js:104:20
    at Array.reduce (native)
    at walkAndMergeNodes (C:\my_projects\myexpressapp\node_modules\pug-walk\index.js:103:18)

2 个答案:

答案 0 :(得分:0)

app.js文件中,检查是否已正确定义视图。

应按照以下app.js

中的说明,将View引擎设置为pug,并正确设置views文件夹。
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));

答案 1 :(得分:-1)