AngularJS和Ex pressJS会议同治? [英] AngularJS and ExpressJS session managment?

查看:115
本文介绍了AngularJS和Ex pressJS会议同治?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保持在所有网页会话。对于这个项目,我使用前presJs,为的NodeJS服务器端。 AngularJS在前端。

I would like to keep session across all the page. For this project, I am using expresJs, nodeJS as server side. AngularJS in front end.

我不知道,该如何处理会话时视图更改或URL变化。因为我需要双方的前presJS路由器或angularJs路由器照顾。

I am not sure, how to handle session when view changes or url changes. Because I need to take care of both expresJS router or angularJs router.

请让我知道,我应该遵循什么样的方式。

Please let me know, what approach should I follow.

angularJS路由器

     myApp.config(['$routeProvider', function($routeProvider) {

    $routeProvider.when('/welcome', {templateUrl: 'partials/welcome.html', controller: 'MyCtrl2'});
    $routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'MyCtrl2'});
    $routeProvider.when('/signup', {templateUrl: 'partials/signup.html', controller: 'singupController'});
    $routeProvider.otherwise({redirectTo: '/'});
  }]);

注册控制器

     myApp.controller('singupController',function($scope,$rootScope,$http){

    $scope.doSingnup = function() {

       var formData = {
          'username' : this.username,
          'password' : this.password,
           'email' : null
      };

      var jdata = JSON.stringify(formData);


      $http({method:'POST',url:'/signup',data:jdata})
      .success(function(data,status,headers,config){

                console.log(data);

      }).
      error(function(data,status,headers,config){

        console.log(data)

      });

    }



  })

前pressJS路由器

    module.exports = exports = function(app, db) {

    var sessionHandler = new SessionHandler(db);
    var contentHandler = new ContentHandler(db);

    // Middleware to see if a user is logged in
    app.use(sessionHandler.isLoggedInMiddleware);


    app.get('/', contentHandler.displayMainPage);



    app.post('/login', sessionHandler.handleLoginRequest);


    app.get('/logout', sessionHandler.displayLogoutPage);


    app.get("/welcome", sessionHandler.displayWelcomePage);


    app.post('/signup', sessionHandler.handleSignup);

    app.get('*', contentHandler.displayMainPage);

    // Error handling middleware
    app.use(ErrorHandler);
}

申请成功后,我想重定向到登录页面。我怎么能做到这一点在上面路由器。以下哪一个我应该用改变应用程序的视图
          1)angularJS的$位置
          2)重定向防爆presJS的

After signup, I would like to redirect to the login page. How can I do that in the above router. which one of the following should I use to change the view of app 1) $location of angularJS 2) redirect of ExpresJS

感谢所有

推荐答案

这里有两个不同的概念 - 服务器端的会话状态和对角客户端用户状态。在EX preSS您可以通过req.session使用会话管理基于会话的数据。

There are two different concepts here - server side session state and the user state on the client side in Angular. In express you can use the session via req.session to manage session based data.

在角边,只有在控制器范围内。如果你想跟踪跨多个控制器的一些数据,你需要创建一个服务来存储数据,并注入服务为您所需要的控制器。

On the angular side, there is only scope in your controllers. If you want to keep track of some data across multiple controllers, you need to create a service to store the data in and inject the service into the controllers you need.

一个典型的生命周期是首先检查是否有数据已在服务,如果是这样使用它。如果没有,等待填充数据(由用户或应用程序或其他),然后检测这些变化,并与服务同步。

A typical lifecycle is to first check if there is data already in the service, if so use it. If not, wait for the data to be populated (by the user or app or whatever) then detect those changes and synchronize with your service.

这篇关于AngularJS和Ex pressJS会议同治?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆