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?
-
\$\begingroup\$ the database queries should be done in a separate controller \$\endgroup\$Aaron Goldsmith– Aaron Goldsmith2018年05月02日 03:23:23 +00:00Commented May 2, 2018 at 3:23
1 Answer 1
Looking at users.js, you can see that I tried to DRY things up by creating
sequelizeSuccessHandler
andsequelizeErrorHandler
(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.)
Explore related questions
See similar questions with these tags.