是否可以使用带有Node.js后端的Angular 2 Dart前端? [英] Is it possible to use an Angular 2 Dart frontend with a Node.js backend?

查看:419
本文介绍了是否可以使用带有Node.js后端的Angular 2 Dart前端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划在我的网络应用程序中使用的工具。我想使用Node.js作为服务器后端,因为它有一个对我特别有用的模块。然而,我也想使用Angular 2(Dart)与Polymer.dart在前端。对不起,如果答案应该是显而易见的,但它将如何工作结合这两个部分我的应用程序无缝(没有转换工具),通常做在MEAN堆栈,因为Dart不直接兼容JS? p>

目前没有任何教程或资源演示此组合。在我看来,更常见的用例是让Dart也作为服务器后端。

解决方案

服务Dart客户端应用程序



客户端和服务器可以是两个不相互绑定的应用程序。唯一需要的连接是服务器可以解释客户端发送的请求,客户端可以解释响应。



内置的Dart客户端应用程序静态HTML,可以由任何HTTP服务器提供服务。我不知道Node.js,但我认为它有一个目录,它提供静态内容。这是您放置Dart客户端应用程序的构建输出的位置。



Dart客户端和Node.js服务器之间的通信



为了使客户端和服务器能够进行通信,Dart需要以服务器期望的形式发送请求。您可以使用REST,WebSocket,Ajax与JSON正文或协议缓冲区。
在客户端和服务器上使用相同语言的优点在于,可以在客户端和服务器之间共享序列化或反序列化有线协议格式和序列化/反序列化代码的模型类。这在这种情况下是不可能的。 Dart团队正在从Dart生成JS和TS,这可能最终解决这个问题。如果您使用Protocol Buffers,这也不适用,因为您可以从相同的proto文件生成两种语言的代码。



开发要求



对于开发,你必须考虑你需要两台服务器。 Node.js服务器是您应用程序的实际服务器,还包含 pub服务,用于快速更改和重新加载循环(避免在每次更改后构建到JS)。
这通常由代理(可以是Nginx例如或使用shelf和shelf_proxy包的自定义Dart脚本)将Dart源文件的请求转发到 pub服务和Rest / Ajax / WebSocket请求发送到Node.js服务器。


I am planning out the tools I will use in my web app. I would like to use Node.js as a server backend because it has a module that would be particularly useful to me. However, I would also like to use Angular 2 (Dart) with Polymer.dart in the frontend. Excuse me if the answer should be obvious, but how will it work to combine these two parts of my app seamlessly (and without conversion tools), as is commonly done in the MEAN stack, since Dart is not directly compatible with JS?

There aren't any tutorials or resources currently available that demonstrate this combination. It seems to me the more common use case is to have Dart also act as a server backend.

解决方案

How to serve a Dart client application

The client and server can be two distinct applications that are not bound to each other at all. The only connection that is required is, that the server can interpret the requests sent by the client and that the client can interpret the responses.

The built Dart client application is like static HTML and can be served by any HTTP server. I don't know Node.js, but I assume it has a directory where it serves static content from. This is where you place the build output of your Dart client application.

Communication between Dart client and Node.js server

For client and server to be able to communicate, Dart needs to send requests in the form the server expects. You can use REST, WebSocket, Ajax with JSON body or protocol buffers. An advantage of using the same language on client and server is, that model classes that are serialized to or deserialized from the wire protocol format and the serialization/deserialization code can be shared between client and server. That's not possible in this case. The Dart team is working in generating JS and TS from Dart which might solve this eventually. If you use Protocol Buffers this also doesn't apply because you can generate the code for both languages from the same proto files.

Development requirements

For development you have to consider that you "need" two servers. The Node.js server that is the actual server for you application and also pub serve for fast change and reload cycles (to avoid building to JS after each change). This is usually done by a proxy (can be Nginx for example or a custom Dart script using the shelf and shelf_proxy package) that forwards requests for Dart source files to pub serve and Rest/Ajax/WebSocket requests to your Node.js server.

这篇关于是否可以使用带有Node.js后端的Angular 2 Dart前端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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