MEAN 堆栈:角度路由与快速路由 [英] MEAN stack: angular routing vs express routing

查看:27
本文介绍了MEAN 堆栈:角度路由与快速路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始在我的快速生成项目中使用 angular,我很喜欢它.最近我在我的一个测试项目中实现了角度路由,我想知道在纯快速/节点路由上使用角度路由的优缺点是什么(例如,这种方式是否存在技术问题,或者可能是 SEO,还是完全不必要).

I've started using angular in my express generated projects and i'm loving it. Recently I implemented angular routing into one of my test projects and I'm wondering what the advantages and disadvantages are to using angular routing over pure express/node routing (e.g. are there technical issues with this way, or maybe SEO, or is it totally unnecessary).

我的设置仅供参考:我已经快速渲染主索引模板并将所有请求(全部)路由到主索引模板,然后我使用 angular 从主索引模板中路由到部分模板

FYI on my setup: I have express rendering the main index template as well as routing all request (a catch all) to the main index template and then I'm using angular to route to partial templates from within the main index template

推荐答案

使用均值堆栈(mongo、express、angular),您将在两端都有路由.

With the mean stack (mongo, express, angular) you will have routing at both ends.

Express 将为您的静态 index.html 和 css/js/images 以及您的 api 提供服务,而 angular 将与 api 交互以从 mongo 获取数据.

Express will serve your static index.html and css/js/images and your api, and angular will interact with the api to get data from mongo.

使用 express 的路由将主要为 API 完成,而在 angular 中的路由将完成以处理您的应用程序的前端.除了 index.html 和您为 angular 编写的任何模板部分之外,Express 不会返回任何 html,但是即使使用 gulp/grunt 等构建工具将所有模板直接编译到您的 js 文件中,也可以消除这种情况.

Routing with express will primarily be done for the API, and routing in angular will be done to handle the front-end of your application. Express will not return any html other than the index.html and any template partials that you have written for angular, however even that can be eliminated by compiling all of the templates directly into your js files using a build-tool such as gulp/grunt.

当然可以 100% 使用一侧或另一侧,但是这是不切实际的,因为您最终会低效地使用一侧或另一侧.例如,如果您在每个单独的页面上使用 express 和 used angular 完成所有路由,您将忽略所有单页应用程序功能和 angularjs 的路由,留下它的唯一目的是构建可能做得更多的页面有效地使用 express 和 jade(或任何其他模板引擎).实际上不可能朝另一个方向前进并使用 angular 执行所有路由,因为 angular 需要一个 api 来从中获取数据,除非您包含所有数据在 html 中预先内联,我相信你会同意这是一个坏主意.(在这一点上它也消除了 mongo..)

It certainly is possible to go 100% to one side or the other, however, it's impractical because you'll end up inefficiently using one side or the other. For example, if you did all of your routing with express and used angular on each individual page, you would be ignoring all of the single page app functionality and routing of angularjs, leaving it's only purpose being building the page which could probably be done more effectively with express and jade (or any other templating engine.) It isn't really possible to go in the other direction and do all of your routing with angular because angular requires an api to get data from, unless you include all of the data up front inline in the html, which i'm sure you'll agree is a bad idea. (it also eliminates mongo at that point..)

这篇关于MEAN 堆栈:角度路由与快速路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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