如何使用Node.js构建静态和动态内容之间的混合页面? [英] How to use Node.js to build pages that are a mix between static and dynamic content?

查看:82
本文介绍了如何使用Node.js构建静态和动态内容之间的混合页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的5页站点上的所有页面均应使用Node.js服务器输出.

All pages on my 5 page site should be output using a Node.js server.

大多数页面内容是静态的.每页底部都有一些动态内容.

Most of the page content is static. At the bottom of each page, there is a bit of dynamic content.

我的node.js代码当前如下所示:

My node.js code currently looks like:

var http = require('http'); 

http.createServer(function (request, response) {

    console.log('request starting...');

    response.writeHead(200, { 'Content-Type': 'text/html' });

    var html = '<!DOCTYPE html><html><head><title>My Title</title></head><body>';
    html += 'Some more static content';
    html += 'Some more static content';
    html += 'Some more static content';
    html += 'Some dynamic content';
    html += '</body></html>';

    response.end(html, 'utf-8');

}).listen(38316);

我敢肯定,这个例子有很多错误.请赐教! 例如:

I'm sure there are numerous things wrong about this example. Please enlighten me! For example:

  • 如何将静态内容添加到 页面而不用+ =多次将它存储在字符串中作为变量值?
  • 在Node.js中构建一个小型站点(其中所有页面都是静态和动态内容之间的混合)的最佳实践方法是什么?
  • How can I add static content to the page without storing it in a string as a variable value with += numerous times?
  • What is the best practices way to build a small site in Node.js where all pages are a mix between static and dynamic content?

推荐答案

我个人将使用具有更高级别结构的服务器.例如,看一下expressjs框架- http://expressjs.com/

Personally, I'd use a server that has higher level constructs. For instance, take a look at the expressjs framework - http://expressjs.com/

您将对该软件包感兴趣的构造是:

The constructs you'll be interested in from this package are:

  • 真正的静态文件(资产等):app.use(express.static(__ dirname +'/public'));
  • 一种模板语言,例如玉,胡须等:
    • Truly static files (assets etc): app.use(express.static(__dirname + '/public'));
    • A templating language such as jade, mustache, etc:
      • http://expressjs.com/en/guide/using-template-engines.html
      • https://github.com/visionmedia/jade/
      • You'll want to look up 'locals' and 'partials' for embedding small bits of dynamic content in mostly static content

      例如玉器:

      !!! 5
      html(lang="en")
        head
          title= pageTitle
          script(type='text/javascript')
            if (foo) {
               bar()
            }
        body
          h1 Jade - node template engine
          #container
            - if (youAreUsingJade)
              p You are amazing
            - else
              p Get on it!
      

      成为:

      <!DOCTYPE html>
      <html lang="en">
        <head>
          <title>Jade</title>
          <script type="text/javascript">
            if (foo) {
              bar()
            }
          </script>
        </head>
        <body>
          <h1>Jade - node template engine</h1>
          <div id="container">
            <p>You are amazing</p>
          </div>
        </body>
      </html>
      

      如果您更喜欢一些急速的东西,我想说的是看一下胡须或其他看上去更像常规酱汁html的引擎.

      If you prefer something a little less drastic I would say look at mustache or one of the other engines that looks a bit more like regular-sauce html.

      这篇关于如何使用Node.js构建静态和动态内容之间的混合页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆