WarsawJS - Wzorce projektowe dla aplikacji enterprise w Node.js

Wzorce projektowe dla aplikacji enterprise w Node.js

Authors:
github.com/rootsher
github.com/rkaw92

Aplikacja enterprise

Aplikacja enterprise

Aplikacja enterprise

Aplikacja enterprise

Aplikacja enterprise

Podstawowe bloki budulcowe

podejście tradycyjne

                            function saveFileToMongo(fileName, callback) {
                                fs.readFile(fileName, function (err, data) {
                                    if (err) {
                                        return callback(err);
                                    }
                             
                                    collection.insert({
                                        fileName: fileName,
                                        data: data
                                    }, function (err, result) {
                                        if (err) {
                                            return callback(err);
                                        }
                             
                                        callback();
                                    });
                                });
                            }
                             
                            saveFileToMongo('config', function (err) {
                                if (err) {
                                    throw err;
                                }
                             
                                console.log('Save successful');
                            });
                        

podejście promisowe

                            function saveFileToMongo(fileName) {
                                var promisedRead = nodefn.call(fs.readFile, fileName);
                             
                                var doc = {
                                    fileName: fileName,
                                    data: undefined
                                };
                             
                                return promisedRead.then(function (data) {
                                    doc.data = data;
                             
                                    return nodefn.call(collection.insert.bind(collection), doc);
                                });
                            }
                             
                            saveFileToMongo.done(function (result) {
                                console.log('Save successful');
                            });
                        

Podstawowe bloki budulcowe

Warstwa składania aplikacji

podejście tradycyjne

                mongodb.MongoClient.connect('url', 'testDB', function (err, database) {
                    if (err) {
                        throw err;
                    }
                 
                    http.createServer(function (req, res) {
                        database.collection('user').find().toArray(function (err, findResult) {
                            if (err) {
                                throw err;
                            }
                 
                            res.end(JSON.stringify(findResult));
                        });
                    });
                });
            

rozwiązanie promisowe - z wykorzystaniem modułu app-compositor

                function database() {
                    var connectionURL = 'mongodb://localhost:27017';
                    var databaseName = 'testDB';
                 
                    this.provides(function () {
                        var MongoClient = mongodb.MongoClient;
                         
                        return nodefn.call(MongoClient.connect.bind(MongoClient), connectionURL, databaseName);
                    });
                }
                 
                function web() {
                    this.requires('database');
                    this.provides(function (dependencyGetter) {
                        var database = dependencyGetter('database');
                 
                        http.createServer(function (req, res) {
                            database.collection('user').find().toArray(function (err, findResult) {
                                if (err) {
                                    throw err;
                                }
                 
                                res.end(JSON.stringify(findResult));
                            });
                        });
                    });
                }
                 
                manager.runModules([database, web]).done(function applicationCompositionFinished(results) {
                    console.log('Composition finished! The application is now running.');
                }, function handleCompositionError(error) {
                    console.error('Composition error:', error);
                });
            

Styl API

Architektura procesów

Architektura procesów

Niezawodność

this.presentation().then(function () {

when.js - https://www.npmjs.org/when
app-compositor - https://www.npmjs.org/app-compositor
wire.js - https://www.npmjs.org/wire
amqplib - https://www.npmjs.org/amqplib
zmq - https://www.npmjs.org/zmq

}).done(function () {
      console.log('Dziękujemy za uwagę!');
});