使用Express进行AJAX通话

I am trying to append HTML dynamically with Express framework in a static HTML file that my server serves. I've found about the cheerio module that does exactly what I want, but I was wondering if there is a much cheaper way for the system instead of loading the whole HTML and appending a string.

I searched about AJAX and how to communicate with the client but I didn't manage to make it work. The code I am using with cheerio is:

exports.modify = function(req, res){

  var html = fs.readFileSync(__dirname + '/../public/index.html', 'utf8'); 
  var $ = cheerio.load(html);
  var scriptNode = '<p>Source code modified</p>';
  $('body').append(scriptNode);

  fs.writeFile(__dirname + '/../public/index.html', $.html(), function (err) {
    if (err) throw err;
    console.log('It\'s modified!');
    });

  res.send($.html());

}; 

How can I do it in more 'proper' way (maybe with AJAX call)? Any suggestions would be more than welcome.

Assuming you want to handle JSON as a data type then you can setup another specific route or you can filter the request type within the current route handler :

exports.index = function(req, res) {
  var data = someData.fetch();
  switch(req.format) {
    case 'json':
      res.json(data);
      break;
    default:
      res.render('template', {
      data:data
    });
  }
};