【问题标题】:need the logical meaning of app.set('views' , path.join(__dirname , 'views'))需要 app.set('views' , path.join(__dirname , 'views')) 的逻辑含义
【发布时间】:2021-02-23 17:41:40
【问题描述】:

这是服务器,我使用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'))

是否意味着将views文件夹连接到应用程序??

【问题讨论】:

    标签: node.js express pug


    【解决方案1】:

    基于express 文档

    app.set(名称,值)

    将设置名称分配给值。您可以存储任何您想要的值,但某些名称可用于配置服务器的行为。这些特殊名称列在应用设置表中

    所以当你想使用pug 时,你应该在根目录中创建一个子目录来保存.pug 模板文件,比如/views

    还有两行额外的代码要插入到我们的 Express.js 主文件中,都涉及具有 viewsview engine 属性的 app.set() 方法。 views 属性指向保存模板的目录(或目录数组),view engine 属性指定要使用的模板引擎

    【讨论】:

    • 你也能解释一下吗? app.use('/static' , express.static('static')) //用于提供静态文件 谢谢
    猜你喜欢
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    相关资源
    最近更新 更多