Relating Nodejs modules to Services Angularjs - Electron

Asked

Viewed 170 times

1

Hello, Everybody!
I’m starting a study in Electron and in this application I’m using Angularjs, the detail is that I don’t mean much about, and I can’t think of a way to use the Node without the server running, in order I would like to use the modules of the Node in a service of Angular. I’ll try to go into more detail with 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 my view controller (usuarioController.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 that, I’d be very grateful!

1 answer

0


You have to consider the architecture proposed by Electron.

Note that the Node code, your service in the case, must be running main process (ipcMain). Consider this code your back-end which, in turn, should not depend on Angular.

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

Communication between them is through messages/events. You must send a message as[synchronous] of renderer to the main. And send the answer to [synchronous] the other way.

I implemented, partially, an application using Electron and java. The source code is in github. You can explore it and use it as a reference.

  • Thanks @josivan , I think it’s actually better if I give a better study of the framework (Electron), because I really imagined that it just built the desktop application.

  • @Lucashenriquedeabreu, the idea is that you make a web application run on the desktop on the platforms where Electron is available. Electron is a chromiun with built-in Node.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.