Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

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?
like image 350
edt Avatar asked May 25 '11 15:05

edt


People also ask

How do I serve a static page in node js?

In your node application, you can use node-static module to serve static resources. The node-static module is an HTTP static-file server module with built-in caching. First of all, install node-static module using NPM as below. After installing node-static module, you can create static file server in Node.

How do I create a static website with node js?

Download Node. js installable setup from its official site and install with default options. After installation, you will be able to run node and npm on the command line. This command will initialize the folder to be a node project and create package.


1 Answers

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.

like image 190
Josh Avatar answered Sep 21 '22 17:09

Josh