【问题标题】:How do I convert a folder of static html files to be served by node/express through jade?如何将静态 html 文件的文件夹转换为 node/express 通过jade提供的服务?
【发布时间】:2016-03-09 00:11:31
【问题描述】:

作为现有项目的一部分,我有一个包含静态 html 文件的文件夹。我想找到最简单的方法来通过 node/express 使用玉来提供这些服务。顶部有一个公共的head/menu/header部分,我想去掉并放入玉模板中,一个js的公共部分包含在底部也应该在模板中,我想发送来自服务器的几个变量(例如用户)。我非常感谢如何做到这一点的模式,它表明:

  • 渲染的路由和控制器以及如何使用节点设置相应的静态服务
  • 玉文件中的内容部分
  • 静态文件的存储位置
  • jade 如何将具有相同类/ID 的块链接在一起。

谢谢。

【问题讨论】:

标签: node.js express pug


【解决方案1】:

您可以使用fs 读取静态文件,然后通过res.render('page', {contentHTML: data}) 传递每个文件的data。在客户端页面中,使用!= 运算符取消转义数据并将其注入视图。

例如:

static.html(假设文件在views文件夹中)

<div>
  <p> some text </p>
</div>

服务器:(假设此代码在routes 文件夹中)

router.get('/static', function(req, res){
    fs.readFile('./views/static.html', function(err, data){
        res.render('page', {responseHtml: data});
    });
});

page.jade:(假设这个视图在views文件夹中)

extends layout

block content
  div!= responseHtml

应用结构是这样的:

-example
  ...
  -routes
     index.js (here is the code show above, in the Server section)
     users.js
  -views
     ...
     page.jade (client code show above)
     static.html (content to load in page.jade)
  app.js
  package.json

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 2017-07-12
    相关资源
    最近更新 更多