Relate Nodejs modules with Services Angularjs - Electron

1

Hello, everyone! I am starting a study in electron and in this application I am using angularJS, the detail is that I do not understand much about, and I can not think of a way to use the Node without the server being running, I would finally like to use the node modules in an Angular service . I'll try to get a better look at the sources:

I have my user.model.js:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var UserSchema = new Schema({
  name: String,
  password: String
});

module.exports = mongoose.model('User', UserSchema);

In my userService.js:

(function () {
    'use strict';    
    var express  = require('express'),
        mongoose = require('mongoose'),
        User     = require('./user.model.js')

    angular.module('app')
        .service('userService', ['$q', UserService]);

    function UserService($q) {
        return {
            create: createUser
            /*getUsers: getUsers,
            getById: getUserById,
            destroy: deleteUser,
            update: updateUser*/
        };

        function createUser(user) {
            var deferred = $q.defer();
            User.create(user, function(err, res) {
                if(err) { eferred.reject(err); }
                deferred.resolve(res);
            });
            return deferred.promise;
        }
    }
})();

And in the controller of my view (userController.js)

(function () {
    'use strict';
    angular.module('app')
        .controller('UsuarioController', ['userService', '$q', UsuarioController]);

    function UsuarioController(userService, $q, $mdDialog) {
        var self = this;
        self.User = {};
        self.saveUser = saveUser;

        function saveUser($event) {
            userService.create(self.User).then(function (res) {
                console.log('Salvou' + res);
            });
        }
    }

})();

If anyone knows how I can do this, I'll be very grateful!

    
asked by anonymous 10.02.2017 / 18:22

1 answer

0

You have to consider the architecture proposed by electron.

Notice that the node code, your service in this case, must be running the main process ( ipcMain ). Consider this code your backend which, in turn, should not depend on the Angular.

On the other hand, the Angular code will be running in the renderer process ( ipcRenderer ). If you have an Angular service here, you call another service on the back end.

The communication between them is through messages / events. You must send a [synchronous] message from renderer to main . And send the response to [synchronous] the other way.

I have partially implemented an application using electron and java. The source code is at github . You can explore it and use it as a reference.

    
10.02.2017 / 18:32