#Engineering #Fintech Engineering

Creating Your First Microservice (retaillooptube)

Olusola Akinsulere · March 17, 2024 · 4 min read

Starting with our first microservice, we would be using the following tools, git, node.js and visual studio code, feel free to use any alternative for node.js and visual studio code

Why Node.js

In this post, we leverage Node.js for building microservices, a choice motivated by both personal preference and a series of strategic advantages. Node.js's suitability for microservice architecture is highlighted by its network-oriented, high-performance nature, facilitating the development of numerous services efficiently. This efficiency is critical as we delve into creating multiple microservices, making the development process smoother.

Node.js's popularity and extensive ecosystem offer substantial support, easing the learning curve and providing resources for ongoing development. Its design for building lightweight, high-performance services enables the rapid creation of HTTP servers, essential for quick microservice bootstrap. This aligns with our goal of developing multiple small services effectively.

The versatility of JavaScript, used in Node.js, allows for full-stack development capabilities, extending from backend microservices to front-end web development, desktop applications (via Electron), mobile apps (through Ionic), embedded systems (IoT devices), and data manipulation. This minimizes the need for mental context switches when moving across different areas of application development.

Additionally, Node.js's ecosystem is bolstered by npm (Node Package Manager), a tool that provides access to a vast repository of code libraries. This facilitates the integration of various functionalities into our microservices, significantly enhancing development efficiency.

Node.js is not only a strategic choice for its technical merits but also benefits from its open-source nature, with its code available on GitHub, ensuring transparency and community-driven improvements.

In summary, our selection of Node.js for microservice development is underpinned by its performance, flexibility, and supportive ecosystem, alongside the convenience offered by npm. This combination positions Node.js as an ideal platform for efficient and scalable microservice architecture.

I will assume you can install these tools by yourself and for your own OS (Mac, Linux, Windows)

Building an HTTP Server for Video Streaming

We are going to use express and node to setup up the simple HTTP server for the video streaming.
1. Initialize a node project using (npm init -y)
2. Install Express.js (npm install express)
3. Install axios (npm install axios)
4. Create and index.js in the root folder of your application

In this post, we are going to be using a video from sample video, and use axios to stream the content, before passing it down to our servers, we’re doing this because we don’t want to store the huge mp4 on our file system and because potentially we could have replicas, that video might not be found on the filesystem of the other replicas, asides reading from a url, this could be solved by storing the video in a blob storage and reading the file from the blob storage. Here is an example code of the content of the index.js

const express = require("express");
const axios = require("axios");
const app = express();

if !(process.env.port) {
 throw new Error("Please specify the port number")
}

const PORT = process.env.port

app.get("/video", async (req, res) => {


  const url = "https://sample-videos.com/video321/mp4/720/big_buck_bunny_720p_1mb.mp4"; // Replace this with your video URL

  try {

    const response = await axios({

      method: 'get',

      url: url,

      responseType: 'stream'

    });

    res.writeHead(200, {

      "Content-Type": "video/mp4",

    });

    response.data.pipe(res);

  } catch (error) {

    console.error("An error occurred", error);

    res.sendStatus(500);

  }

});

app.listen(port, () => {

  console.log(`Example app listening on port ${port}!`);

});

The code presented in the above code block serves as an illustrative example of utilizing Node.js for streaming capabilities. This process involves using the axios library to get the video as a stream, we could have used the fs library to stream the video, if the video was local on our filesystem. The core functionality is established through defining a REST API endpoint specifically designed for video streaming purposes.

Error handling mechanisms are diligently incorporated to manage potential issues that may arise during the file streaming process. The culmination of these operations is the initiation of a readable stream from the video file, seamlessly piped through to the HTTP response utilizing the `pipe` function. This methodical approach establishes a direct conduit for byte-by-byte video streaming, enabling efficient content delivery to the browser.

The integration of Node.js and Express significantly simplifies the construction of this streaming pipeline, showcasing the ease with which sophisticated video streaming services can be developed and deployed. This example underscores the powerful streaming capabilities inherent in Node.js, facilitated through concise and effective coding practices.

Conclusion

If you don’t want to wait for the whole series to end to get the knowledge, you can buy my ebook on gumroad => Microservices . To encourage to keep up the pace with writing this blog post, you can buy me coffee with the button below

Buy Me Coffee

Originally published on substack

Enjoyed this?

Subscribe to get new essays in your inbox.