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.
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.
Discussion
View additional discussion.