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

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

问题描述

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

目前没有任何可用的教程或资源来演示这种组合.在我看来,更常见的用例是让 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 serve 用于快速更改和重新加载周期(以避免在每次更改后构建到 JS).这通常由代理(可以是 Nginx 或使用shelf 和shelf_proxy 包的自定义Dart 脚本)完成,它将Dart 源文件的请求转发到pub serve 和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.

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

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