将HTML5与AngularJs和Node/Express以及Mean.js样板使用



我使用的是eyan.js(http://meanjs.org/generator.html)样板作为应用程序的起点,因为我喜欢InsturiTCHATICATION/授权。

但是,我在使用HTML5时遇到了问题。

在我的Angular应用程序中,我设置了HTML5(true),并且我知道我需要为所有其他请求重定向设置一个CatchAll路线。

我在Express上有以下路线为根和catchall:

///这是app/controllers/core.server.controller:

exports.index = function(req, res) {
    res.render('index', {
        user: req.user || null,
        request: req
    });
};
exports.catchall = function(req, res){
      res.redirect('/');
};

,然后路由本身

'use strict';
module.exports = function(app) {
    // Root routing
    var core = require('../../app/controllers/core.server.controller');
    app.route('/').get(core.index);
    app.route('*').get(core.catchall);
};

现在,当我输入仅垃圾的路由时,这些页面正在重定向,但是当我输入Express中存在的路由时(没有关联的视图,我将获得服务器输出)。

这是我的意思是express:

'use strict';
/**
 * Module dependencies.
 */
var users = require('../../app/controllers/users.server.controller'),
    articles = require('../../app/controllers/articles.server.controller');
module.exports = function(app) {
    // Article Routes
    app.route('/articles')
        .get(articles.list)
        .post(users.requiresLogin, articles.create);
    // Finish by binding the article middleware
    app.param('articleId', articles.articleByID);
};

我在Express/Node中没有与此相关的视图 - 仅在角度。

因此,当我导航到localhost:3000/文章通过链接,Angular管理路线并呈现正确的角模板。

但是,当输入本地主机:3000/文章,然后按Enter(或在此URL上刷新浏览器),我会得到以下内容:

[{"_id":"5555ede9dac9d0d99efae164","user":{"_id":"5554fa21141c5aef7b0354d7","displayName":"heny as"},"__v":0,"content":"This is the blurb for him","title":"VP, Global Marketing","created":"2015-05-15T13:00:25.177Z"}]

,但我想从Angular

获得渲染的页面模板

任何人可以帮忙吗?

这是因为您调用服务器URL(与Ajax调用相同),您需要

  1. intercept请求在服务器端(ajax |而不是ajax)中要么要么将所有ajax重定向到root路径(/),而且Angular将使用Ajax将文章置于客户端。

  2. 定义用于服务单个Web应用程序的单个根(/),并使用(/api/...)处理所有AJAX请求。

最新更新