问题描述
我们的应用程序将有一个网站和一个移动应用程序,它们都与同一个 API 后端通信.我有一个 Nodejs 应用程序仅用于提供 API,另一个 Nodejs 应用程序为网站提供 html 页面.我正在为这两个应用程序使用 Expressjs Web 框架.
Our application will have a website and a mobile app both communicating to the same API backend. I have one Nodejs application for serving only APIs and a second Nodejs app serving html pages for the website. I am using Expressjs web framework for both of these apps.
从另一个 Nodejs 应用程序调用一个 Nodejs 中的 API 有哪些不同的方法?关于何时使用每种方法的附加信息会很棒.
What are different methods to call APIs in one Nodejs from another Nodejs app? Additional information on when to use each method would be great.
例如,我有以下应用程序
Example,I have the following applications
- NodejsAPI(节点和快递)
- Nodejs 网站(节点和快递)
- 移动应用
NodejsAPI 将为 MobileApp 和 NodejsWebsite 提供对 API 的访问.MobileApp 将通过 http 访问 API.但我想知道 NodejsWebsite 在 NodejsAPI 应用程序中调用 API 的选项是什么.据我了解,这将是两个进程之间的进程间通信.对于 .net 应用程序,可以使用 .net 管道、tcp 通信等完成此类通信. unix 和 linux 平台上的 Nodejs 应用程序的等效方法是什么?
NodejsAPI will provide access to APIs for the MobileApp and the NodejsWebsite. MobileApp will access APIs over http. But I want to know what are the options for NodejsWebsite to call APIs in NodejsAPI app. From what I understand this will be inter process communication between the two processes. For .net applications such communications could be done using .net pipes, tcp communication etc. What are the equivalent methods for Nodejs applications on unix and linux platforms?
从 IPC 的角度思考,我发现以下内容很有用,
什么是最高效的 node.js 进程间通信库/方法?https://www.npmjs.org/package/node-ipc
Thinking from IPC perspective I found the following to be useful,
What's the most efficient node.js inter-process communication library/method?https://www.npmjs.org/package/node-ipc
推荐答案
有 node 的 vanilla http 客户端,http客户端瑞士军刀,请求,然后是superagent,类似于 jQuery.ajax.为了让您的生活更轻松,有 扶手 和 fementa,同一事物的两种不同风味.
There's node's vanilla http client, http client swiss army knife, request, then there's superagent, similar to jQuery.ajax. To make your life easier there's armrest and fementa, both different flavors of the same thing.
现在,如果您想获得更高的性能并拥有应用程序的另一个接口,您可以使用以下 RPC 解决方案之一:
Now if you want to reach for more performance and have another interface of your application, you can use one of these RPC solutions:
dnode:最受欢迎的解决方案之一.它使事情变得非常容易.它使使用远程接口变得无缝.phantomjs-node 使用 dnode.与其他物体相比,在处理大型物体时表现不佳.对于小东西,它是完美的.还有其他语言的其他端口.
dnode: One of the most popular solutions. It's makes things very easy. It's makes using remote interfaces seamless. phantomjs-node uses dnode. Doesn't perform well with huge objects compared to others. For small stuff, it's perfect. There's other ports for other languages too.
zerorpc:使用 zeromq 因为它是以可靠着称的套接字库.它也支持连接到 python 客户端.
zerorpc: Uses zeromq as it's socket library which is famous for being reliable. It supports connecting to a python client too.
smith:cloud9 编辑器后端使用的 RPC 系统.基本上几乎和 dnode 一样好,但速度更快.smith 和 zerorpc 都使用 msgpack 而不是 JSON,因此它们将节省网络上的字节.
smith: RPC systems used in cloud9 editor backend. Basically almost as nice as dnode, but faster. Both smith and zerorpc uses msgpack instead of JSON, so they will save bytes on the wire.
axon-rpc:轻量级解决方案.和 zerorpc 一样好用.您可以将其配置为将 msgpack 与 axon-msgpack 一起使用.
axon-rpc: A lightweight solution. As nice to use as zerorpc. You can configure it to use msgpack with axon-msgpack.
以上所有工作都适用于 TCP(要在不同的机器上使用)或 Unix 域套接字(比 TCP 快,但只能在同一台机器上).
All of above work on both TCP(To be used on different machines) or Unix Domain Sockets(faster than TCP, but only on the same machine).
如果您想要更高的性能,您可以将您的 NodejsAPI 嵌入您的 NodejsWebsite,只需简单地要求它的接口模块即可.
If you want more performance, you can embed your NodejsAPI in your NodejsWebsite, by just simply requiring it's interface module.
如果您想要比这更好的答案,请写一个更具体的问题.这个问题实在是太宽泛了.
If you want answers better than this, write a more specific question. The question as it is, is too broad.
这篇关于从另一个 Nodejs 应用程序调用一个 Nodejs 应用程序中的 API 的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!