Nodejs microservices. From Zero to Hero. Pt2 (Basic usage)

Erich Oliveira
Microservices Practitioner Articles
4 min readMay 6, 2016

--

This is part 2 of a series of posts on motivations for microservices on nodejs and how to implement it using Studio . You can read Part 1 here.

On this new post we will finally start to implement our microservices the final code for this post is available on github here , I will keep the main project with a branch for each new post, so the branch for this post is pt2.

We are going to implement a simple web application, it is basic, but will help us to see the features and advantages of Studio, on this post we will keep it simple, and on the next we are going to start using plugins and clusterize our application and you will see how we can do big improvements with really small changes.

On reddit there is a subreddit exclusive for videos (https://www.reddit.com/r/videos) on this subreddit you can search for the newest, the top among other options. Our application consists on a web server listening on port 3000, and when you get a path it will fetch data from reddit, search for this videos on youtube and show title and description, so, if you hit in your browser http://localhost:3000/new it needs to fetch title and description info for all new youtube videos posted on the subreddit videos. Really simple.

For the implementation I will be using node 4 and some es6 features (class and generators), this wayI can better show you how Studio can also help with code readability. Studio is es6 ready, but is completely compatible with es5 and is able to run on node >= 0.11 . When using es6 features i will also add a gist on how to do this on es5 (or older node versions).

So, lets start creating our application, to do this create a folder named example-studio-microservices (or anything you want), navigate to the folder and type:

npm init

You can keep hitting enter to choose the default options, then execute the following commands to install the dependencies we will need.

npm i express —-save
npm i request —-save
npm i snoocore —-save
npm i studio —-save

We will use express for our http server, request to call youtube api, snoocore is the library for reddit api and studio for our services.

First we will implement the service to fetch video from youtube (you will need a google account and an api key that for youtube data api, you can get it here). Create a file called youtube_service.js with the following code:

On the code above we can see some cool features from studio. First studio supports async calls through generators out-of-the-box, normally yield would wait for promises response, but studio have the method Studio.defer which allows you to easily support the old callback interfaces (without the need of promisify all your code).

The other important thing to notice is, studio services ALWAYS returns a promise, even if you’re just returning a “sync object” (imagine an url from other website, we would return null , but studio handle this so the caller will receive a promise instead of the sync object, this way you don’t release zalgo)

And for the last you will notice we won’t module.exports anything, this happens because studio enables you to create a completely decoupled application, studio have an internal route to deal with service calls.

Ok, this is cool, now we can go to reddit code, create a file called reddit.js, I’m not going on details on this files this is just basic instantiation of snoocore library, you only need to change the credentials with your own reddit credentials (you will need to create a user on reddit and create a new reddit app here , mark it as a script) and type the following code:

And lets implement the reddit services on a file called reddit_service.js:

Here the key is to understand that we don’t need to require the youtube_service.js , as i said you are building decoupled services. Also you will see that you can create several services for a class easily. Another cool feature is the yield for an array of promises, this way you can execute concurrent code easily. And again we exports nothing.

Now we only have to create our server, create a file called express.js:

Nothing new on this file, for the last, we need a file to make everything work together (you do need to require all files so studio can load your services), I like to create it in a different file so I can easily create different builds for the project (all services together or groups of service per machine), as we are in the basic, lets create an all_services.js file:

The last important thing here is to note that Studio really don’t mind about orders, you probably remember that reddit service (searchForContent) calls youtube fetch service, so you would expect some kind of order, but studio handles it for you, you can even require ./express for last, you can play with the require order and see it working no matter what.

To test your application you can run node all_services.js on your terminal and open http://localhost:3000/new on your browser.

Thats enough for today, on the next post i will teach how to use plugins this way we will create a cluster, log realtime metrics, add timeouts and retries.

See you.

Es5 versions of the files:

youtube_service.js

reddit_service.js

express.js

Pt3 (plugins and clustering) is already available at: https://medium.com/@ericholiveira/nodejs-microservices-from-zero-to-hero-pt1-plugins-and-clustering-ddb60e9a8ee0#.qfm0hayb3

Wants to learn more? Join our slack channel:https://studiojs.herokuapp.com/

--

--