为什么app.engine没有默认设置为layout.hbs

时间:2018-11-18 16:10:42

标签: node.js express controller handlebars.js

我用layout.hbs创建了一种样式格式,并注意到所有页面在样式上没有相同的一致性。我注意到我没有声明应用程序。 app.js中的引擎。接下来,我实现了app.engine代码以设置在layout.hbs

中实现的默认布局
app.engine('hbs', hbs ({extname: 'hbs',defaultLayout: 'layout'}));

发生错误,指出无法找到layout.hbs。我再次实现了代码,因为我注意到我没有将文件夹目录定向到布局.hbs。所以我将代码实现为

app.engine('hbs', hbs ({extname: 'hbs',defaultLayout: 'layout', layoutsDir:__dirname + '/app_server/views'}));

错误消失了,设置为layout.hbs的默认布局结构仅显示在本地服务器索引上,并且在所有控制器中都不相同。我不太确定我还缺少什么

控制器和路由代码

var express = require('express');
var exphbs = require('express-handlebars');
var router = express.Router();
var ctrlCaribbeanIslands = require('../controllers/CaribbeanIslands')



/* GET home page. */
router.get('/', ctrlCaribbeanIslands.login);
router.get('/blog/add',ctrlCaribbeanIslands.addPost);
router.get('/chat/add',ctrlCaribbeanIslands.addChat);
router.get('/review/add',ctrlCaribbeanIslands.addReview);
router.get('/traceYourTravel', ctrlCaribbeanIslands.tracetravel);


**module.exports = router;**

//controller 
module.exports.login = function (req, res) {
  res.render('index', { title: 'login'});
};

module.exports.addPost = function(req, res){
  res.render('index', { title: 'Add Post' });
};

module.exports.addChat = function(req, res){
  res.render('index', { title: 'Add Chat' });
};

module.exports.addReview = function(req, res){
  res.render('index', { title: 'Add Review' });

};
module.exports.tracetravel = function(req, res){
  res.render('index', { title: 'Trace travel' });

};

app.js

var createError = require('http-errors');
var express = require('express');
var handlebars = require('hbs');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var hbs = require ('express-handlebars');


var indexRouter = require('./app_server/routes/index');
var usersRouter = require('./app_server/routes/users');

var app = express();

app.engine('hbs', hbs ({extname: 'hbs',defaultLayout: 'layout', layoutsDir:__dirname + '/app_server/views'}));
app.set('views', path.join(__dirname, 'app_server','views'));
app.set('view engine', 'hbs');
app.use(express.static(path.join(__dirname, 'public')));

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

2 个答案:

答案 0 :(得分:0)

我认为Express不再包含把手,我建议使用npm下载HBS,然后要求它var hbs = require('hbs')

然后将视图引擎设置为这样的hb-> app.use("view engine", "hbs")

尝试到目前为止,它已经对我有用,而不是从快速车把上要求它。

答案 1 :(得分:0)

@Zrushb对不起,我不好,它与app.set一起使用,未使用。代码应如下所示:

const express = require("express"),
hbs = require("hbs"),
mongoose = require("mongoose")
bodyParser = require("body-[arser")

var app = express()
//In ** your database if you need any
mongoose.connect("mongodb:localhost:27017/**")


app.set("view engine", "hbs")

//To get or post

app.set("view engine", "hbs") //Engine HBS
app.set("views", __dirname +"/views") //Folder views (templates)
app.use(express.static("public")) //Public is static (to get .js, images and .css)
app.use('/css',express.static(path.join(__dirname, 'public/stylesheets'))); //Css folder specified (NOT WORKING)
app.use(bodyParser.urlencoded({extended: false})) //Post Body Parser
app.use(bodyParser.json()) //Parser for JSON files
app.use(cookieParser())
hbs.registerPartials(__dirname+ "views/partials")


app.get("/" //etc)

app.post(//etc)