The user can specify which source to get the list, if the file is not found then Boom returns 404. After that, the found source will fetch the top list and append source, query and data in the response. It's simple as that. I'm just wondering if this code is clean enough to the industry standard.

This application is written in Hapi and using Boom. My code is really simple but I figure there must be something I can do to make it simpler.

  handleRequest: function handleRequest(request, reply) {
    var source = request.params.source;
    var handler;
    try {
      handler = require('../services/' + source);
    } catch (err) {
      err.message = 'Unknown Resource: ' + source;
      return reply(Boom.notFound('Source unknown', err));

    .then(function thenFunc(obj) {
      var response = {};
      if (source) {
        response.source = source;
      if (obj.query) {
        response.query = obj.query;
      response.data = obj.list;
    .catch(function catchFunc(topErr) {
      return reply(Boom.create(500, 'Error', {message: topErr}));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.