简体   繁体   English

将HTML5与Angularjs和Node / Express以及MEAN.js样板一起使用

[英]Using HTML5 with Angularjs and Node/Express and the MEAN.js boilerplate

I'm using the mean.js ( http://meanjs.org/generator.html ) boilerplate as a starting point for an app, as I love the inbuilt authentication/authorization. 我使用mean.js( http://meanjs.org/generator.html )样板作为应用程序的起点,因为我喜欢内置的身份验证/授权。

However, I'm having a problem with using HTML5. 但是,我在使用HTML5时遇到问题。

In my angular app i've set HTML5(true) and I know that I need to set up a catchall route for all the other requests to be redirected. 在我的角度应用程序中,我已经设置了HTML5(true)而且我知道我需要为所有其他要重定向的请求设置一个全面的路由。

I have the following routes on my express as the root and catchall: 我的快递单上有以下几条路线,分别是root和catchall:

///this is the app/controllers/core.server.controller: ///这是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('/');
};

And then the routing itself 然后路由本身

'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);
};

now the pages are redirecting no problem when I enter routes that are just garbage, but when I enter a route that exists in express (with no associated view I'm getting server output). 现在,当我输入只是垃圾的路由时,页面重定向没有问题,但是当我输入在express中存在的路由时(没有关联的视图,我将获取服务器输出)。

Here is the route i mean for 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);
};

I have no view associated with this in Express/Node - just in Angular. 我在Express / Node中没有与此相关的视图-仅在Angular中。

so when I navigate to localhost:3000/articles via a link the angular manages the route and renders the correct angular template. 因此,当我通过链接导航到localhost:3000 / articles时,角度将管理路由并呈现正确的角度模板。

However, when enter localhost:3000/articles and press enter (or refresh the browser on this url) I get the following: 但是,当输入localhost:3000 / articles并按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"}]

but I want to get getting the rendered page template from angular 但我想从角度获取渲染的页面模板

Can anyone help? 有人可以帮忙吗?

This is because you call the server url (wich is the same used for ajax calls), you need to 这是因为您调用服务器网址(与用于ajax调用的网址相同),因此需要

  1. Either intercept request type in the server side (ajax | not ajax) and redirect all no ajax to root path (/) and angular will use ajax to get articles in client side. 在服务器端截取请求类型(ajax |不是ajax),然后将所有no ajax都重定向到根路径(/),而angular将使用ajax在客户端获取文章。

  2. Define a single root (/) for serving your single web application and use (/api/...) to handle all your ajax requests. 定义一个根目录(/)来服务单个Web应用程序,并使用(/ api / ...)处理所有ajax请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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