(function () {

    var baseApp, onCreate, onAppend;

    module.exports = function (_onCreate, _onAppend) {
        onCreate = _onCreate;
        onAppend = _onAppend;
    };

    module.exports.merged = ['view engine', 'views'];

    module.exports.create = function (app) {
        baseApp = app;
        onCreate && onCreate(app);
        onAppend && onAppend(app);

        return app;
    };

    module.exports.append = function(app, root) {

        module.exports.merged.forEach(function (merged) {
            var baseAppValue = baseApp.get(merged);
            if (baseAppValue !== undefined) {
                app.set(merged, baseAppValue);
            }
        });

        onAppend && onAppend(app);

        if (arguments.length > 1) {
            baseApp.use(root, app);
        }

        return app;
    };

}());

(function () {


    module.exports = function (onCreate, onAppend) {
        var baseApp;

        return {
            /**
             * Array of properties to be copied from main app to sub app
             */
            merged: ['view engine', 'views'],

            /**
             * Array of properties to be copied from main app to sub app locals
             */
            locals: [],

            /**
             * Register the main express application, the source of merged/locals properties
             *
             * @param app
             */
            create: function (app) {
                baseApp = app;
                onCreate && onCreate(app);
                onAppend && onAppend(app);

                return app;
            },

            /**
             * Register a sub application, when the root is supplied the app is also bound to the main app.
             *
             * @param {string} [root]
             * @param app
             */
            route: function(root, app) {
                if (arguments.length === 1) {
                    app = root;
                    root = null;
                }

                module.exports.merged.forEach(function (merged) {
                    var baseAppValue = baseApp.get(merged);
                    if (baseAppValue !== undefined) {
                        app.set(merged, baseAppValue);
                    }
                });

                module.exports.locals.forEach(function (local) {
                    app.locals[local] = baseApp.locals[local];
                });

                onAppend && onAppend(app);

                if (root > 1) {
                    baseApp.use(root, app);
                }

                return app;
            }
        };
    };

}());

Rename to use route instead of append, allow multiple main apps per node instance
Rename to use route instead of append, allow multiple main apps per node instance
