1
\$\begingroup\$

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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 4, 2015 at 5:24
\$\endgroup\$
1
  • \$\begingroup\$ the database queries should be done in a separate controller \$\endgroup\$ Commented May 2, 2018 at 3:23

1 Answer 1

1
\$\begingroup\$

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.)

answered Oct 4, 2015 at 5:38
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.