saymedia / angularjs-server

Specialized server for AngularJS sites
MIT License
247 stars 49 forks source link

Access to module from nodejs #42

Closed daweedm closed 6 years ago

daweedm commented 6 years ago

Is it possible to define new services or factories directly from the "nodejs context" ? I would like to access to node modules to embed some of theses into services

apparentlymart commented 6 years ago

Hi @daweedm,

It's been a while, but IIRC you can pass functions in the angularModules array that get injected in the provider phase while constructing an injector, which you can then use to register services into the injector. Something like this:

var angularMiddlewares = angularserver.Server({
    template: template,
    serverScripts: [
        'angular.js',
        'angular-route.js',
        'yourapp.js'
    ],
    clientScripts: [
        '/static/angular.js',
        '/static/angular-route.js',
        '/static/yourapp.js',
        '/static/clientonly.js'
    ],
    angularModules: [
        function ($provide) {
            $provide.factory('example', function () {
              // ...
            });
        },
        'yourapp'
    ]
});

Since this function is defined within a NodeJS module, it can access the local variables available in that module, which includes the variables containing the results of any require(...) calls that you can use to bind to NodeJS modules.

daweedm commented 6 years ago

Thank you very much!