Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Build microservices in Node.js with micro


    micro is a small module that makes it easy to write high performance and asynchronous microservices in Node.js. This video will introduce you to micro by building a tiny service that responds to all requests.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    First, we npm-install the micro module with the -g global flag. This will give us access to the micro command, which we'll use later on to start our micro service. Then we create an index.js file, which we will write our first micro service into.

    In this file, we require the send function from micro. This is the main function we'll use when writing your micro services. It allows you to send responses back to the client. To write our micro service, all we have to do is export the function, which gets two arguments, the request that comes in and the response object, which we can send back to the client.

    Let's do that. We use the send function to send the response object with an http status code of 200, which means a successful request, and a body. In our case, we'll say, "Hello, world." We'll save this and start our first micro service by typing micro index.js.

    This starts our micro service. As you can see, we can access it at 0000and then port 3000. If you open this in your browser, you will see that our micro service responds with our "Hello, World" message.