简体   繁体   English

NodeJS包括没有EJS的页眉和页脚

[英]NodeJS include header and footer without EJS

I was hoping someone may be able to assist me with NodeJS. 我希望有人可以帮我解决NodeJS问题。 Iv recently started developing my site using NodeJS. Iv最近开始使用NodeJS开发我的网站。

The problem: 问题:

im looking for a way to send my documents as a header, content, footer style so it reduces repeating code for each section. 即时寻找一种方式将我的文档作为标题,内容,页脚样式发送以减少每个部分的重复代码。

I have a public folder setup so i can include Js, CSS and common files etc... 我有一个公用文件夹设置,所以我可以包括Js,CSS和常见文件等...

self.app.use(express.static(__dirname + '/public'));

This all works, however im unsure about how to implement a simple way to join the file content. 这一切都有效,但我不确定如何实现加入文件内容的简单方法。 Every source iv looked at online suggests the "correct" way to do it is via a package called EJS templates. 静态查看在线的每个来源都表明“正确”的方法是通过一个名为EJS模板的软件包。

So im looking for something similar to this implementation in EJS: 所以我在EJS中寻找类似于这个实现的东西:

<header>
    <% include ../partials/header %>
</header>

<footer>
    <% include ../partials/footer %>
</footer>

However, the service im hosting on does not support EJS, so is it possible to do something like this? 但是,托管的服务不支持EJS,那么可以做这样的事情吗?

self.routes['/'] = function(req, res) {
            res.setHeader('Content-Type', 'text/html');
            res.send(self.cache_get('header.html')+self.cache_get('index.html')+self.cache_get('footer.html') );
        };

Is there another simple way to separate header, content and footer to make it simpler to develop website with dynamic content? 是否有另一种简单的方法来分隔标题,内容和页脚,以便更容易开发具有动态内容的网站?

While you could write your own js to do this, at your current level I don't recommend it (other than for learning and experimental purposes). 虽然您可以编写自己的js来执行此操作,但在目前的级别我不推荐它(除了用于学习和实验目的)。 This is why node is so powerful, because you don't have to reinvent the wheel all the time. 这就是节点如此强大的原因,因为您不必一直重新发明轮子。 You noted that you are using openshift, and that you cannot use modules. 您注意到您正在使用openshift,并且您无法使用模块。 This is where you are incorrect . 这是你不正确的地方

This link explains how to use npm with openshift. 此链接说明如何将npm与openshift一起使用。 It would be extremely silly for any node.js hosting service to not allow package integration. 任何node.js托管服务都不允许包集成,这将是非常愚蠢的。 To briefly explain how this works, you essentially install your packages in your local environment using the --save flag. 要简要说明这是如何工作的,您实际上是使用--save标志在本地环境中安装软件包。 This will update your local package.json file with details about your dependencies and versions. 这将更新您的本地package.json文件,其中包含有关您的依赖项和版本的详细信息。 When you push your repo to your host, it looks at this file, and takes care of the dependency installation for you. 当您将repo推送到主机时,它会查看此文件,并为您处理依赖项安装。

On this note, if you are going to continue using node.js, you should have a good read about package management. 在本文中,如果您要继续使用node.js,您应该对包管理有一个很好的阅读。 https://docs.npmjs.com/how-npm-works/packages https://docs.npmjs.com/how-npm-works/packages

Trumpet should let you do this. 小号应该让你这样做。 Something like this may work: 这样的事情可能有用:

const trumpet = require('trumpet')
const tr = trumpet()

tr.pipe(res)

const header = tr.select('header').createWriteStream()
fs.createReadStream('header.html').pipe(header)

const footer = tr.select('footer').createWriteStream()
fs.createReadStream('footer.html').pipe(footer)

fs.createReadStream('index.html').pipe(tr)

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

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