简体   繁体   中英

where and how to use nodejs with angularjs

I am new to UI framework development. Currently my requirement is to work with anuglarjs and nodejs. I know there are few more like me who want to know the exact use..

I am confused as where i need to use nodejs in my application. I tried to find a simple live demo example(plunker/jsfiddle) which used angularjs and nodejs but i couldn't find one.

Currently i have written a small module using angularjs, which hits the java controller class and saves/get the data and display the data on the webpage. Here i'm no where using nodejs. I tried to search in web to understand the use of nodejs with angularjs. Any input's would be very helpful.

Below is the sample javascript code i implemented using angularjs.

myDataCOntroller.js

//some code here

$scope.submitFormData = function(myForm){
        if(myForm.$valid)
         {

            MyDataService.saveOrGetData($scope.myReport).then(
                    function(response) {
                        $scope.myReport = response;
                    },
               function(errResponse){
                   console.error('Errorr');
                });

         }else{
            console.log("invalid form data!!");
         }
    }

myDataService.js

app.factory('myService',function($http,$q,$location){

    var MY_SERVICE_URI = $location.protocol()+'://'+$location.host()+':'+$location.port();

    var _repServiceFactory={};


    _repServiceFactory.saveOrGetData = function(myData){

         var deferred = $q.defer();
         var url = appURL+'/saveOrGetData.form';
         $http.post(url,JSON.stringify(myData))
            .then(
            function (response) {
                   deferred.resolve(response.data);
            },
            function(errResponse){
                console.error('Error while fetching data');
                deferred.reject(errResponse);
            }
        );
        return deferred.promise;
  }

PS: I know that Angularjs is client side, Nodejs is server side, both are using Javascript programming language. What i want to know is, what is the use of nodejs and where is it used in real time??

If you are writing an AngularJS front end web application, you may never have to use NodeJS.

If you need tooling (build scripts to compile Sass, linters, etc) in your development or deployment process you can use NodeJS task runners like Gulp , Grunt or Webpack .

If you need to build a back end API, that stores and retrieves information, you can use Express or the whole MEAN stack .

* * * Also worth noting-- you mention hitting a Java class. If you are already using Java for the backend, then you probably won't be using any NodeJS for that purpose. If you were just shortening JavaScript to "Java", please note they are separate and distinct languages. * * *

Web dev has 4 primary parts:

For you to create an app that is realtime, you can use websockets, especially Socket.io ( which works really well with and is built for Node.js )

WebSockets are essentially bi-directional ( low latency ) event-based tunnels where data can be passed from client-to-server , and server-to-client without the need to make an AJAX ( tcp ) request per message.


This paired with a strong data-binding architecture ( API -> Angular Binding ) allows for customizable single page views, where the same data can be used to create a view, and update the back-end modal when the data gets updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM