node.js - 需要 app.set('views' , path.join(__dirname , 'views')) 的逻辑含义
问题描述
这是服务器,我使用pug
模板engine init
const express = require('express');
const app = express();
const path = require('path')
const port = 80;
//Express specific stuff
app.use('/static' , express.static('static')) //For serving static files
//Pug specific stuff
app.set('view engine', 'pug'); //set the template engine as pug
app.set('views' , path.join(__dirname , 'views')) //set the views directory
//End points
app.get('/' , (req , res)=>{
res.status(200).render('index.pug')
})
//start the server
app.listen(port , ()=>{
console.log(`The application is running at port${port}`)
})
我想知道的只是角色是什么
app.set('views' , path.join(__dirname , 'views'))
这是否意味着将视图文件夹连接到应用程序?
解决方案
基于快速文档
app.set(名称,值)
将设置名称分配给值。您可以存储任何您想要的值,但某些名称可用于配置服务器的行为。这些特殊名称列在应用设置表中
所以当你想使用时,pug
你应该在根目录中创建一个子目录来保存.pug
模板文件,比如/views
.
有两行额外的代码要插入到我们的 Express.js 主文件中,都涉及app.set()
带有属性的方法views
和view engine
. 该views
属性指向保存模板的目录(或目录数组),并且该view engine
属性指定要使用的模板引擎
推荐阅读
- css - Bootstrap 可以在桌面上的第 2 列旁边堆叠第 1 列和第 3 列吗?
- ios - 新创建文档的子集合上的“权限缺失或权限不足”
- java - Javafx:多线程提醒通知
- c - C 指针错误,得到 exc_bad_access 代码=2,地址 0x100000000
- django - 在 Django 中迭代表单的值
- python - Django 2.1 - DeleteView - 只有所有者可以删除或重定向
- python - UDP 服务器客户端未发送完整文件
- django - 不安全的重定向到 URL
- python - Django DetailView 无法解析视图的 slug
- jquery - 形式化引导程序 4