angular - 是否建议客户端路由与服务器端路由一起提供 angular-universal

标签 angular angular2-routing isomorphic-javascript angular-universal

我刚刚开始使用 Angular Universal 并且对“同构”javascript 完全陌生。

我对如何拥有一个逻辑路由系统有点困惑,你是想构建一个典型的 node rest api 的 angular universal 吗?或者它的字面意思是立即为客户端代码提供服务,并从外部源/rest api 检索数据?

以angular universal starter kit为例,就是从这些路由开始的;

app.get('/', ngApp);
app.get('/about', ngApp);
app.get('/about/*', ngApp);
app.get('/home', ngApp);
app.get('/home/*', ngApp);

然后在旁边包含一个节点/express/mongo api来获取/发布数据是否合乎逻辑?即;

const bearRoutes = require('./routes/bear')
app.use('/', bearRoutes)

如果是,您会使用哈希策略来区分 URLS 吗?

与将所有内容托管在一起相比,使用来自外部 API 的通用数据在 angular2 应用程序上提供数据是否会有明显的性能差异?

感谢您的任何建议!

最佳答案

I'm a little confused as to how to have a logical routing system, are you meant to build out angular universal as a typical node rest api?

是的。 angular-universal 采用了这种理念。您在服务器端构建路由,因为一切都是在服务器端构建的。

is it then logical to include a node/express/mongo api along side this to get/post data?

是的,适用于您的数据服务。 例如Hello World example with github services makes API requests app.use 不行,因为这实际上是您的 Node REST 服务器。

If it is, would you then use a hashing strategy in order to differentiate the URLS?

首先,服务器端不会看到 URL 中 # 之后的任何内容。但是您描述的问题是关于命名 URL 以及是否区分为 Angular 编译/呈现的 HTML 内容提供服务的 URL 与数据/API 服务。

答案是肯定的。这是为了您自己管理服务器路由。例如,如果您的路线是

app.get('/', ngApp);
app.get('/about', ngApp);
app.get('/about/*', ngApp);
app.get('/home', ngApp);
app.get('/home/*', ngApp);

你可以添加

app.get('/api/profile', serverAPIAppOrprofileJSON); 

Would there be a noticeable performance difference between serving data on an angular2 app using universal from an external api compared to having everything hosted together?

考虑这样一种情况,您的页面的一小部分由于某些用户交互而发生变化,并且需要您提供一些数据。您可能只想更新此部分而不是呈现整个页面。所以在这种情况下性能会更好。

关于angular - 是否建议客户端路由与服务器端路由一起提供 angular-universal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40266164/

相关文章:

angular - 如何将名称绑定(bind)到路由器 socket

javascript - react 引用错误: document is not defined

node.js - require 客户端实现同构 javascript 与 React 无法识别

server - 如何在没有浏览器同步的情况下提供 angular2 应用程序

angular - 试图了解 CanActivate 和 CanActivateChild 之间的区别

Angular 7 (RxJs 6.x) - 如何使用 throwError

angular - 如何在 Angular2 中做响应式组件

jquery - 在 angular 2 中使用 jquery 小部件的最佳方法是什么?

Angular2 - ngOnDestroy() 未在类似路线上调用

javascript - 将服务器端渲染与客户端路由 react