How to use Node.js to build pages that are a mix between static and dynamic content?

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:

  • 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

For example in jade:

!!! 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!

Becomes:

<!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>

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)