重构快速路由

时间:2015-06-15 14:01:13

标签: node.js express

我有许多表达类似功能的快速路由,但处理特定功能的.put除外。

重构此类代码的最佳方法是什么。

需要声明

var Ale        = require('../models/alert-model.js');
var Service    = require('../models/services-model.js');

路线

router.route('/ale/:_id')
.get(function(req, res) {
    Ale.findById(req.params._id, function(err, result) {
        if (err)
            res.send(err);
        res.json(result);
    });
})
.put(function(req, res) {
        Ale.findById(req.params._id, function(err, ale) {
            if (err)
                res.send(err);
            ale.title = req.body.title;
            ale.visible = req.body.visible;
            // save the items
            service.save(function(err) {
                if (err)
                    res.send(err);
                res.json({ message: 'Service updated!' });
            });
        });
    })
.delete(function(req, res) {
        Ale.remove({
            _id: req.params._id
        }, function(err, service) {
            if (err)
                res.send(err);

            res.json({ message: 'Successfully deleted' });
        });
    });  



router.route('/ser/:_id')
.get(function(req, res) {
    Service.findById(req.params._id, function(err, result) {
        if (err)
            res.send(err);
        res.json(result);
    });
})
.put(function(req, res) {
        Service.findById(req.params._id, function(err, service) {
            if (err)
                res.send(err);
            service.title = req.body.title;
            service.shortname = req.body.shortname;
            service.contents = req.body.contents;
            service.category = req.body.category;
            service.bubbleimage = req.body.bubbleimage;
            service.visible = req.body.visible;
            // save the items
            service.save(function(err) {
                if (err)
                    res.send(err);
                res.json({ message: 'Service updated!' });
            });
        });
    })
.delete(function(req, res) {
        Service.remove({
            _id: req.params._id
        }, function(err, service) {
            if (err)
                res.send(err);

        res.json({ message: 'Successfully deleted' });
    });
});

1 个答案:

答案 0 :(得分:2)

Create a small util lib for yourself, which will act as a function factory.

Take those two

router.route('/ale/:_id')
.get(function(req, res) {
    Ale.findById(req.params._id, function(err, result) {
        if (err)
            res.send(err);
        res.json(result);
    });
})

router.route('/ser/:_id')
.get(function(req, res) {
    Service.findById(req.params._id, function(err, result) {
        if (err)
            res.send(err);
        res.json(result);
    });
})

You could do this

var utils = {};

utils.readById = function(oType, then){
    return function(req, res) {
        oType.findById(req.params._id, function(err, result) {
            if(then) then(err,res);
            if (err)
                res.send(err);
            res.json(result);
        });
    }
}

Then do

router.route('/ser/:_id')
.get(utils.findById(Service));

Then add more personnalization according to your need.