设计 Javascript 前端 <->C++ 后端通信

2022-01-19 00:00:00 node.js backend frontend javascript c++

在不久的将来,我将不得不制作一个带有 C++ 后端和 Web 前端的系统(要求).目前,我对此知之甚少.我认为前端将触发数据传递,而不是后端 - 所以不需要类似 Comet 的东西.

In my nearest future I will have to make a system with C++ backend and web frontend (requirements). At the moment, I don't know much more about it. I think that Frontend will be triggering data delivery, not backend - so no need for Comet-like things.

由于可能在该领域的经验很少,非常感谢您对我所做的设计决策的评论.

Because of possibly little experience in this field, I'd really appreciate your comments about design decisions I made.

首先,我不喜欢从 C++ 生成 HTML 的选项.因此,C++ 后端必须与 Javascript 前端进行通信.我在这里看到的最简单的选择是 Ajax.到目前为止,我认为应该没问题.

First of all, I don't like the option of generating HTML from C++. So, C++ backend will have to communicate with Javascript frontend. Simplest option I see here is Ajax. I think it should be ok, so far.

通过 Ajax 与 C++ 后端通信意味着后端应该能够处理 HTTP.最好将提供实际数据的后端与 HTTP 处理功能分开.

Commucating through Ajax with C++ backend means that backend should be capable of handling HTTP. It'd be nice to separate backend which provides actual data from HTTP handling functionality.

在这里我看到了 Node.js 的位置.我对此进行了概述,这就是我所有疑问的所在.

Here I see the place for Node.js. I got an overview of it and this's the place where all my doubts lie.

在 Node.js 上拥有一个 HTTP 处理服务器,它将数据后端"作为 Node.js 模块?我认为,应该没问题 - 但我不确定我是否真的需要所有这些异步,所以可能有一些我不知道的更简单的选项?您将如何制作这样的系统?

To have a HTTP handling server on Node.js, which will have the 'data backend' as a Node.js module? I think, it should be ok - but I'm not sure that I really need all this asynchronization, so there may be some simpler options I'm not aware of? How would you make such a system?

提前致谢.

推荐答案

所有这些异步"并不是 Node.js 努力提供的额外功能.一旦您了解了 Node.js 的工作原理,这是一种不同的 Web 服务视图.

"All this asynchronization" is not something that Node.js works very hard to provide as an extra. It is a different view of Web serving that is easy as breathing once you understand how Node.js works.

例如,我的同事需要一种将 C++ 程序包装为 Web 服务的方法,但该程序的启动成本很高,因此他们只想运行该程序的一个实例,循环运行,提供服务所有的网络请求.Node.js 中的整个过程不到两屏.

For example, my colleagues needed a way to wrap a C++ program as a web service, but the program had a significant start-up cost, so they wanted to run just one instance of the program, running in a loop, serving all the web requests. The whole thing in Node.js took less than two screenfuls.

包装为每个请求调用的单个程序可以在不到十行 Node.js 中完成.不要认为异步是一件苦差事――如果你接受它,Node.js 就很棒.

Wrapping a single program that is called for each request can be done in less than ten lines of Node.js. Don't think of asynchronicity as a chore - if you embrace it, Node.js is awesome.

也就是说,您可以走 CGI 路线,并以更标准的方式进行,最终结果将几乎相同.这个可能会也可能不会派上用场.

That said, you could go the CGI route, and do it in a bit more standard way, and the end result would be pretty much the same. This may or may not come in handy.

相关文章