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

Posted by edt on Stack Overflow See other posts from Stack Overflow or by edt
Published on 2011-05-25T15:02:46Z Indexed on 2012/10/02 9:38 UTC
Read the original article Hit count: 148

Filed under:

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.

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:

  • 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?

© Stack Overflow or respective owner

Related posts about node.js