Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

best way to call own api in nodejs

best way to call own api in nodejs

Problem

If to call own api for building the website is a good practice. Which is the best way to call own api on the same server in a nodejs application?

  • simply calling the api-methods
  • using socket.io with emit() and listen it with .on('event', function(){})
  • install jquery on the server and use the ajax call
  • or not use at all the own api and rewrite the methods

i'm just confusing. Hope someone can clarify me on this.

Problem courtesy of: Sasha Grey

Solution

If you need to call own API from another process it would be good to use some messaging protocol. ZeroMQ sounds like perfect fit here. It allows to create different patterns of communication between different services in internal networks, and communicate in different ways. Simplest example is Request > Response pattern that is similar to HTTP requests. And it might be a good start point.

Remember that if you using routing system within express, then ZeroMQ solution will not utilize that, it would be able directly communicate, not through HTTP interface. It is much more efficient as well, as HTTP has unnecessary overhead especially for internal communication.

If you still want to use Express Routing then your option would be to use http.request, which behaves very similar to curl or $.ajax. This function makes HTTP requests, so you can reuse your express routing system.

Solution courtesy of: moka

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

best way to call own api in nodejs

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×