Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a very simple Node application that routes requests to /users to the following file:

users.js

var express = require('express');
var router = express.Router();
var User = require('models/user');
var sequelizeSuccessHandler = require('lib/sequelizeSuccessHandler');
var sequelizeErrorHandler = require('lib/sequelizeErrorHandler');

router.get('/', function(request, response) {
  User.findAll()
  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));
});

router.post('/', function(request, response) {
  User.create(User.params(request.body))
  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));
});

router.get('/:id', function(request, response) {
  User.findOne({where: {id: request.params.id}})
  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));
});

router.patch('/:id', function(request, response) {
  User.findOne({where: {id: request.params.id}})
  .then(function(user) {
    return user.update(User.params(request.body));
  })
  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));
});

router.delete('/:id', function(request, response) {
  User.findOne({where: {id: request.params.id}})
  .then(function(user) {
    return user.destroy();
  })
  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));
});

module.exports = router;

lib/sequelizeSuccessHandler.js

var Sequelize = require('sequelize');

function successHandler(response) {
  return function(object) {
    response.send(JSON.stringify(object));
  };
}

module.exports = successHandler;

lib/sequelizeErrorHandler.js

var Sequelize = require('sequelize');

function errorHandler(response) {
  return function(error) {
    if (error.constructor == Sequelize.ValidationError) {
      response.status(400).send();
    } else {
      response.status(500).send();
    }
  };
}

module.exports = errorHandler;

Looking at users.js, you can see that I tried to DRY things up by creating sequelizeSuccessHandler and sequelizeErrorHandler (I hate those names), but there's still a lot of repetition. I feel like there is a better way to do this.

How can I clean this up?

share|improve this question

Looking at users.js, you can see that I tried to DRY things up by creating sequelizeSuccessHandler and sequelizeErrorHandler (I hate those names), but there's still a lot of repetition.

I suppose you're talking about these repeated lines in the route handlers:

  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));

I don't really know Node, but it seems to me that you could add another helper function:

function handle(query, response) {
  query
  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));
});

router.get('/', function(request, response) {
  handle(User.findAll(), response);
});

router.post('/', function(request, response) {
  handle(User.create(User.params(request.body)), response);
});

(The names "handle", and "query" might not be the best.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.