3

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

Community
  • 1
  • 1
maulik13
  • 3,656
  • 1
  • 25
  • 36
  • Are there any reasons why you don't choose a more efficient way? Is the website rendered server-side (e.g. Jade templates) or is it a single-page web application (AngularJS, jQuery, Backbone, Ember.js, ...)? – hgoebl May 24 '14 at 13:43
  • 1
    It is not an SPA website. It's going to be rendered through some template system on the server side. The point is to keep API development separate from the website development and hence two separate nodejs apps. – maulik13 May 24 '14 at 17:59
  • You can still do separate development on the same app in that scenario, just use the right tools. – Zlatko May 25 '14 at 06:41
  • @Zlatko Forgot to expand on separation part. I meant not just separate development but they will be separate projects with their own repositories, versioned and deployed differently as well. – maulik13 May 25 '14 at 07:48
  • 2
    But it's still doable, depending on yor use case. Let's say you develop the API part as a lib. Then you'd have to develop your webapp and simply include the api as another module, from a git subrepo or npm or something similar. But of course, there are cases where you actually need to separate the code because of other concerns. I'm just saying this to state there's a possibility. – Zlatko May 25 '14 at 08:04
  • @Zlatko thank you for expanding on it. I am new to Nodejs and NPM so I am not sure of all the options available to me. Could you please add this as an answer and a small example of how it would work? – maulik13 May 25 '14 at 08:12

1 Answers1

6

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.

Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
  • I could definitely make an HTTP request to the API from my website application. Thank you for providing some links of different modules to aid the process. I have also expanded a bit on my description. – maulik13 May 25 '14 at 08:24
  • Still looking the same to me. `REST === flavored JSON HTTP requests`, if you haven't designed your API and the NodejsAPI is only gonna be used by NodejsWebsite and if it doesn't have to be REST then I can suggest different stuff. – Farid Nouri Neshat May 25 '14 at 08:30
  • NodejsAPI is going to be used by a mobile application as well so it has to serve data via http. – maulik13 May 25 '14 at 08:33
  • Then that's all there is to this question. – Farid Nouri Neshat May 25 '14 at 08:35
  • Well, I was not clear in my question earlier. REST interface is only for MobileApp, and the website does not have to use REST interface to access the APIs. So besides making http requests directly there must be other ways. – maulik13 May 25 '14 at 09:02