A
A
a6963852014-03-12 23:16:06
Angular
a696385, 2014-03-12 23:16:06

Write an article on the unusual practice of writing services in Node.js?

I'm not sure if it's worth writing an article on non-traditional Node.js development practices. I wanted to cover writing a Restfull service using the Dependency Injection pattern, all asynchrony through Promise (Q module), annotations for declaring Http methods, validations, etc. in a word, we refuse middleware and write annotations to methods. On the example of todoMVC with Angular.js, i.e. using this practice to write a backend for Angular.js.
Write if you are interested to see the article, thanks.

Looks like this style
/**
 * @HttpMethod('/album')
 */
define('album', __filename, function ($errors, $mongodb, albumModel, imageModel) {
    var ObjectId = $mongodb.ObjectID;
    return {

        /**
         *
         * @Validation(fields='name')
         * @HttpMethod(method='POST')
         */
        new: function(req, res){
            var name = req.param('name', '');
            return albumModel.insert(req.user, name).then(function(album){
                return res.send(204, '');
            });
        },

        /**
         *
         * @Validation(fields='albumId,name', type='album')
         * @HttpMethod(url='rename', method='POST')
         */
        rename: function(req, res){
            var name = req.param('name', ''),
                albumId = req.param('albumId', '');

            return albumModel.update({_id: new ObjectId(albumId)}, {name: name}).then(function(){
                    return res.send(204, '');
                });
        },
/**
         *
         * @Validation(fields='albumId', type='album')
         * @HttpMethod(url='remove', method='POST')
         */
        remove: function(req, res){
            var albumId = req.param('albumId', '');

            return albumModel.findOne({_id: new ObjectId(albumId)}).then(function(album){
                if (album.isOther) throw new $errors.HttpError(401, res.locals.__('messages.accessDenied'));
            }).then(function(){
                    return imageModel.remove({albumId: new ObjectId(albumId)}).then(function(){
                        return albumModel.remove({_id: new ObjectId(albumId)});
                    }).then(function(){
                            return res.send(204, '')
                        });
                })

        },
   };
});

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-03-12
@a696385

why are dependency injection and deferred objects unusual in the context of js?
And so write of course, but try to reveal everything in more detail, why use this and that and profit from this. Describe inversion of control in js, there are all sorts of features... examples... The tutorial article on how to write it is not so interesting, it's more like an analysis of a finished application with a detailed description of these practices. Well, what would people be able to see on a real example.

A
Alexander Litvinov, 2014-03-12
@Sander_Li

It would be interesting

Z
zxmd, 2014-03-12
@zxmd

You do not have enough adventures to see, since you are writing such code ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question