Methods for calling APIs in one Nodejs app from another Nodejs app

…衆ロ難τιáo~ 提交于 2019-12-05 02:55:49

问题


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.

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.

EDIT:

Example, I have the following applications

  • NodejsAPI (node & express)
  • NodejsWebsite (node & express)
  • MobileApp

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?

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


回答1:


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.

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: 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: Uses zeromq as it's socket library which is famous for being reliable. It supports connecting to a python client too.

  • 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: A lightweight solution. As nice to use as zerorpc. You can configure it to use msgpack with axon-msgpack.

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).

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.



来源:https://stackoverflow.com/questions/23844727/methods-for-calling-apis-in-one-nodejs-app-from-another-nodejs-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!