我有一组文件(HTML 片段),我正在使用 Node.js 提供这些文件,如下所示:
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)
这提供了一个索引,其中包含指向 HTML 文件的事件链接列表。我需要将链接的 HTML 文件包含在模板中(即,在每个页面上放置相同的 header ,并读取相同的 css 文件)。当我使用 documentation 中所述的 template
选项时,唯一提供的内容是模板:
// Serve directory indexes for public/ftp folder (with icons)
var index = serveIndex('./', {'icons': true, template:'template.html'})
如何使用模板或其他配置在默认 HTML 中提供 HTML 片段?
最佳答案
我认为您需要在模板中添加一些内容才能实际显示如下文件:
<body class="directory">
<div id="wrapper">
<h1><a href="/">~</a>{linked-path}</h1>
{files}
</div>
</body>
如果您查看/public 下的serve-index 安装目录,您会发现名为directory.html 的默认模板文件。您可以剪切并粘贴一些代码来显示您的文件。
关于javascript - 使用 Node.js 将 HTML 内容插入模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30471014/