node.js - 如何强制 Angular 在 HTML5 模式下向服务器发送请求?

标签 node.js angularjs oauth oauth-2.0 angular-routing

我正在尝试使用 angular 和 nodejs 构建和 web。我在 /home 路径上加载 Angular,其中 / 包含登录和注册表单。这是我的 Angular 配置:

window.app.config(['$routeProvider', '$locationProvider',
    function($routeProvider,$locationProvider) {
        $locationProvider.html5Mode(true);
        $locationProvider.hashPrefix('!');
        $routeProvider.
        when('/profile', {
            templateUrl: '/views/account.html',

        }).
        when('/edit', {
            templateUrl: '/views/edit.html',

        }).
        when('/home', {
            templateUrl: 'views/index.html'
        }).
        when('/signout', {
            templateUrl: 'views/signout.html'
            //on this view i load a controller which submits a form to /signout
        }).

        otherwise({
            redirectTo: '/home'
        });
    }
]);

在服务器端路由:

    app.get('/',function(){
          res.render('index',{
             user: req.user? req.user:'guest'
        });
    });
    app.post('/login',function(){
             //if success redirect to /home
             //if fails redirect to /
    });
    app.get('/auth/facebook', passport.authenticate('facebook', { scope: [ 'email',                  'user_about_me', 'read_stream', 'publish_actions'], failureRedirect: '/' }));
  app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/' }), users.authCallback);
    app.get('/signout',function(){
             //signing out the user here...and redirects to /
    });
    app.get('/home',function(req,res){
        res.render('users/home',{
          user: req.user? req.user:'guest',
          message: req.flash('error')

        })
      }); 
    app.get('/profile',function(req,res){
        res.render('users/home',{
          user: req.user? req.user: 'guest',
          message: req.flash('error')

        })
      });
      app.get('/edit',function(req,res){
        res.render('users/home',{
          user: req.user? req.user:'guest',
          message: req.flash('error')

        })
      });

如果我点击 facebook auth,由于 Angular 路由,我如何通过点击 url /home 上的链接发送请求到 /auth/facebook它把我送到 /home。 Angular 阻止向服务器发送请求,它只是转到 url /home,我尝试通过替换 / 来删除否则 /home。结果是一样的,只是那里没有加载 View 。

最佳答案

来自Docs :

In cases like the following, links are not rewritten; instead, the browser will perform a full page reload to the original link.

  • Links that contain target element. Example: <a href="/ext/link?a=b" target="_self">link</a>

  • Absolute links that go to a different domain. Example: <a href="http://angularjs.org/">link</a>

  • Links starting with '/' that lead to a different base path when base is defined. Example: <a href="/not-my-base/link">link</a>

因此,作为最简单的解决方案,只需添加 target="_self"到您的链接。

关于node.js - 如何强制 Angular 在 HTML5 模式下向服务器发送请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19858475/

相关文章:

node.js - "as"关键字有什么作用?

javascript - Meteor MongoDB 集合无法访问 Meteor 发布

node.js - 使用 sails 时是否需要在 package.json 中显式 grunt

javascript - 只使用一次 Angular Directive(指令)

javascript - Angular - 将指令附加到 future 元素

php - 按国家/地区获取 Twitter 主题标签

java - 基于哈希片段的安全性到底是如何工作的?

javascript - 为什么 onmessage 监听器不处理初始 SSE 事件?

javascript - 如何获取具有相同值的两个对象,并使它们被视为一个对象

php - 在 Lumen 中设置 OAuth2