代码之家  ›  专栏  ›  技术社区  ›  Gilles Heinesch buninadev

我怎样才能用快速车把?

  •  0
  • Gilles Heinesch buninadev  · 技术社区  · 7 年前

    这个问题其实并不难理解, 我不知道如何在快车上安装车把。

    这就是我已经编码的:

    var express = require('express');
    var app = express();
    
    app.get('/', function (req, res, next) {
            return res.render('index');
    });
    

    现在是我的问题,如何将车把设置为express的应用程序引擎?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Gilles Heinesch buninadev    7 年前

    这是我目前使用和学习的代码。我在每一行重要的字后面都加了一个便条,这样你就明白了!

    var express = require('express');
    var app = express();
    var handlebars = require('express-handlebars');
    
    app.engine('handlebars', handlebars({ // Here we define what format you will use (That means what's at the end of each file, for example test.handlebars or test.hbs)
        defaultLayout: 'main', // That's the name of your template file. In my case it's main.handlebars
        layoutsDir: __dirname + '/views/layouts/' // That's the directory where the template file is
    }));
    
    app.set('views', path.join(__dirname, 'views')); // Here you give express the information that it has to look at all files that are in the path /views
    app.set('view engine', 'handlebars'); // Here you say express that you are using handlebars to build your website
    
    app.get('/home', function (req, res, next) { // That's a simple GET request (This GET request gets triggered when you enter http://localhost/home for example)
            return res.render('index');  // Here we render the index.handlebars file (that is in the /views folder)
    });