简体   繁体   English

使用Node.js将HTML内容插入模板

[英]Insert HTML content into template with Node.js

I have a set of files (HTML fragments) that I'm serving up with Node.js as follows: 我有一组要与Node.js一起使用的文件(HTML片段),如下所示:

var finalhandler = require('finalhandler')
var http = require('http')
var serveIndex = require('serve-index')
var serveStatic = require('serve-static')

// Serve directory indexes for all files in the folder
var index = serveIndex('./', {'icons': true})

// Serve up public/ftp folder files
var serve = serveStatic('./')

// Create server
var server = http.createServer(function onRequest(req, res){
  var done = finalhandler(req, res)
  serve(req, res, function onNext(err) {
    console.log(req,res)
    if (err) return done(err)
    index(req, res, done)
  })
})

// Listen
server.listen(3000)

This serves up an index with a list of active links to the HTML files. 这为索引提供了指向HTML文件的活动链接的列表。 I need the linked HTML files to be contained in a template (ie, put the same header on each page, and read in the same css files). 我需要链接的HTML文件包含在模板中(即,在每个页面上放置相同的标题,并读取相同的CSS文件)。 When I use the template option as described in the documentation , the only content that is served up is the template: 当我按照文档中所述使用template选项时,提供的唯一内容是模板:

// Serve directory indexes for public/ftp folder (with icons)
var index = serveIndex('./', {'icons': true, template:'template.html'})

How do I use a template or other configuration to serve up the HTML fragments inside the default HTML? 如何使用模板或其他配置来提供默认HTML中的HTML片段?

I think you need to add something to your template to actually display the files like this: 我认为您需要向模板中添加一些内容才能实际显示如下文件:

<body class="directory">
  <div id="wrapper">
    <h1><a href="/">~</a>{linked-path}</h1>
    {files}
  </div>
</body>

If you take a look at the serve-index installation directory under /public you will find the default template file called directory.html. 如果查看/ public下的serve-index安装目录,则会找到名为directory.html的默认模板文件。 You can cut and paste some of that code to get your files to display. 您可以剪切并粘贴一些代码以显示文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM