Populating an unordered list from values in a JSON file I have this view that populates an unordered list from values in a JSON file, which I am doing in Node.

Is there a better way to do this, or a more 'node-like' way? Can I load the JSON file without an Ajax call?

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;4&amp;middot;pli -- news&lt;/title&gt;
    &lt;link rel='stylesheet' href='/stylesheets/style.css'/&gt;
    &lt;script src="/javascripts/jquery-1.9.1.min.js" type="text/javascript"&gt;&lt;/script&gt;
    &lt;script src="/javascripts/date.format.js" type="text/javascript"&gt;&lt;/script&gt;
    &lt;link href='http://fonts.googleapis.com/css?family=Didact+Gothic' rel='stylesheet' type='text/css'&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;% include shared/nav.ejs %&gt;
&lt;div class="wrapper"&gt;
    &lt;ul class='news'&gt;
       &lt;script type="text/javascript"&gt;
           $.ajax({
               url: 'data/news.json',
               async: false,
               dataType: 'json',
               success: function (response) {
                   var items = [];
                   $.each(response.news,function(i,item){
                       items.push('&lt;li&gt;&lt;ul&gt;&lt;li class="title"&gt;'+ item.title +'&lt;/li&gt;&lt;li&gt;' + dateFormat(item.created_at,"fullDate") + '&lt;/li&gt;&lt;li&gt;&lt;p&gt;'+ item.content +'&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;');
                   });
                   $('.news').append(items.join(''));
               }
           });
       &lt;/script&gt;
    &lt;/ul&gt;

&lt;/div&gt;
&lt;% include /shared/footer.ejs %&gt;
&lt;/body&gt;
&lt;/html&gt;
What you're looking for is JSON.parse. It's not in the Node docs, because it's "lower" than that: Every modern javascript runtime has it (see MDN).

Here's a simple function to read a file, parse it as JSON, and send it to a callback (all in Node)

function readJSONFile(filename, callback) {
  require("fs").readFile(filename, function (err, data) {
    if(err) {
      callback(err);
      return;
    }
    try {
      callback(null, JSON.parse(data));
    } catch(exception) {
      callback(exception);
    }
  });
}


In keeping with Node conventions (and just mirroring readFile itself), you pass it a callback with this signature: function(err, json). E.g.

readJSONFile("path/to/file.json", function (err, json) {
  if(err) { throw err; }
  console.log(json);
});


You can make a none-async one if you prefer, but Node's nature is async.