title: Nodejs - A better way to handle ajax request to the server
body: <p>Now I ran into a problem with managing ajax requests on nodeJS server. Currently I have this system it works, but it's ugly and not that efficient.</p>
  
  <pre><code>router.post('/get', function(req, res) {
    var request = req.body.request;
    if (request == null) {
      res.json({success: 'false', error: 'Response returned null'});
      return;
    }
    if (request == "render_page") {
      var page = req.body.page;
      if (page == null) {
        res.json({success: 'false', error: 'Page returned null'});
        return;
      }
      res.render(page);
    } else if (request == "render_article") {
      var temp = {
        articles: [
          {title: 'ABC', text: 'This is a test article'},
          {title: 'XYZ', text: 'Just another random article'}
        ]
      };
      res.render('template/article-format', temp);
    } else {
      res.json({success: 'false', error: "Unknown request " + request});
    }
  </code></pre>
  
  <p>Is there a better way to do this and even maybe make it dynamic? Also the server likes to crash if something goes wrong so there's that.</p>