Skip to main content

Posts

What is HTTP/2?

HTTP/2 is an extended version of HTTP communication protocol after HTTP/1.1. Targets of the improvement are: Faster communication (50% improvement) Better utilization of available network capacity Keep existing application with older version (HTTP/1.1) running (no changes required) How those targets can be achieved? There are several technical aspects: Applying header field compression Allowing multiple concurrent exchanges on the same connection (allowing interleaving request & response) No change in core concepts of HTTP such as method, status codes, URIs, and headers. HTTP/2 add binary framing layer in HTTP protocol that dictates how messages are encapsulated and transferred. There are several terms in HTTP/2 protocol: Stream. It is bidirectional flow of bytes that can contain one or more messages. Frame. It is smallest unit of messages in stream. It has header that contains identity which stream belongs to. Frame can contain encoded header data or payload data of a request

Run MongoDB in Docker Container

For faster application development and delivery, Docker can be a good choice. MongoDB image has been available in Docker registry. You can run MongoDB in Docker container then connect your application or service to it. To make the MongoDB service becomes available in containers network and its data can be persisted, there are several steps for configuration. 1. Create named-volumes for MongoDB data and configuration file, then create network for containers in your application system. For example, the volumes are my-mongo-data and my-mongo-config , and the network is named my-net . docker volume create my-mongo-data docker volume create my-mongo-config docker network create my-net 2. As mentioned in MongoDB for Docker' site , database data is stored in /data/db directory in the container. MongoDB for Docker also accepts environment variables for setting up initial username and password for root user which are named MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD

My Scariest Sleep Paralysis Moment

This is very old story. It happened when I was still a kid. This experience had made me afraid for opening my eyes in sleep paralysis for years. People may have some experiences in sleep paralysis. Many explanations also has already been available in the internet such as this WebMD article . Based on my own experiences, I have several notions why it can happen. Personally, it can be caused by sleeping position, absence of pillow, and bad sleeping pad. But, I still have no idea for some phenomenons in sleep paralysis moment like what happened to me when I was a kid. It was in fasting month and I was still in fourth grade of elementary school. I felt asleep in front of a television on a bed beside a sofa. I slept on my side facing to the right. At one moment, I suddenly had an urge to wake up, but I couldn't move any of my limbs except my eyelid. I also feel vibration on some areas of my body expecially on armpit. When I opened my eyes, I saw something in just 10 centimeters in fr

Measuring Performance of Node.js Program Using Performance Hook Module

Node.js has provided tools for measuring or benchmarking your application performance by marking certain blocks of codes and capturing duration of the executions. For example, you have a program example.js that run two functions, generate random string and make a request to facebook.com , as follows. const https = require('https'); const debuglog = require('util').debuglog('example'); // 1) generate 100 digits random string let randomStr = ''; let allowedChars = 'abcdefghijklmnopqrstuvwxyz0123456789'; for (let i=0; i<100; i++) { randomStr += allowedChars.charAt(Math.floor(Math.random() * allowedChars.length)); } debuglog('Random String:', randomStr); // 2) make a request to facebook.com let request = https.request({ protocol: 'https:', hostname: 'facebook.com', method: 'GET', path: '/' }, (res)=>{ if (res.statusCode) { debuglog('Request Status Code:', res.statusCode); }

Shipping Node.js-based Application as Docker Image

Docker is a set of tools for managing and delivering services or application components as isolated packages called containers. This technology become more popular in present time along with increase in demand of micro-services architecture, continous integration, and faster features or services delivery. This article shows you how to ship your Node application as Docker image and deploy it as a container. 1. Initiate a Node project. mkdir my-node-app cd ./my-node-app npm init -y 2. Write a sample Node application named server.js in your project directory. const http = require('http'); const chalk = require('chalk'); const server = http.createServer(function(req,res){ res.end('Hello from Node server.'); }); server.listen(5000, ()=>{ console.log(chalk.green('Your server is running on port 5000!')); }); 3. Install your Node application dependency. npm install --save chalk 4. Write a Dockerfile file in your project directory. # s

Getting Host System Status in Node.js

Node.js has been shipped with modules that can enable our application to retrive information about the system status suh as memory usage, platform of OS, number of CPUs, etc. Those modules are OS and V8 . Some usage examples are as follows. const os = require('os'); const v8 = require('v8'); console.log('Platform:', os.platform()), console.log('Number of CPUs:', os.cpus().length), console.log('Free Memory:', os.freemem()), console.log('Current Malloced Memory:', v8.getHeapStatistics().malloced_memory), console.log('Peak Malloced Memory:', v8.getHeapStatistics().peak_malloced_memory), console.log('Size Limit of Heap Memory:', v8.getHeapStatistics().heap_size_limit), console.log('Total Heap Memory:', v8.getHeapStatistics().total_heap_size), console.log('Used Heap Memory:', v8.getHeapStatistics().used_heap_size), Heap memory is space of memory that is allocated for storing dynamic objects in Node.js

Installing Multiple Instances of Linux Distributions in WSL

By support of WSL (Windows Subsystem for Linux), you can install any Linux distros in a Windows machine. Recommended method from WSL documentation is by downloading the distribution from Microsoft Store or find .appx installation file available in Microsoft website. For running multiple instances of same Linux distribution, you can duplicate the data using export-import procedure, as I have mentioned in another post . Another method that might be more beneficial is by utilizing Docker. Currently, Docker has already had variety of images of Linux distributions in its registry. You can also store your own costumized distribution in Docker registry that can be distributed to any machines instantly. After you had WSL 2 and an installed Linux distribution from Microsoft Store, you are ready to have more Linux instances in your Windows. 1. List all installed distributions in your Windows. wsl --list -v 2. Run the distribution you desired from terminal, for example, you have insta

Creating HTTP/HTTPS Server in Node.js

Node.js has become a powerful tool for building many kinds of services on the web. It provides variety of built-in modules that can be utilized to meet any developers needs. One of common modules for web developers is HTTP module. This module can be used for handling HTTP request or building a web server. This following example shows you how to build HTTP and HTTPS server in Node.js. This sample server will accept client request and deliver response with a payload containing what the client was sent. 1. In your project directory, generate self-signed certificates that will be used for configuring HTTPS server. OpenSSL is required for this process. openssl req -nodes -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 2. Create a file named server.js . const http = require('http'); const https = require('https'); const {StringDecoder} = require('string_decoder'); // function that receives request and response parameters from HTTP/HTTPS server

Compress and Decompress File/Text Content using Zlib Module in Node.js

Zlib module provides compression functionality implemented using Gzip, Deflate/Inflate, and Brotli. Compression/decompression can be used by utilizing stream directly or read the text content first. Compressing Text Content const zlib = require('zlib'); let rawStr = '--hello--world--'; let compressedStr = ''; zlib.gzip(rawStr, (err, buffer)=>{ if (!err) { compressedStr = buffer.toString('base64'); } }); Decompressing Text Content const zlib = require('zlib'); let rawStr = ''; let compressedStr = 'H4sIAAAAAAAACtPVzUjNycnX1S3PL8pJ0dUFABFjTmQQAAAA'; let inputBuffer = Buffer.from(compressedStr, 'base64'); zlib.unzip(inputBuffer, (err, buffer)=>{ if (!err) { rawStr = buffer.toString(); } }); Compression Using Stream Pipeline const stream = require('stream'); const fs = require('fs'); const zlib = require('zlib'); stream.pipeline(fs.createReadStream(&qu

Managing WSL Data Directory/Storage

WSL (Windows Subsystem for Linux) is a great tool provided by Windows 10 (OS build 20262 or higher) that enables developers to run Linux machines in Windows platform without third-party application like Virtual Box. With support of installed WSL, you can install any Linux distribution available in Microsoft Store . After installation of a Linux distribution, for example Ubuntu 18.04, WSL stored its machine data in %APPDATA%\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState . If you need to move the data to another directory/drive in your computer to free some spaces in your main drive, you can perform these following commands. 1. List all distributions in your computer. wsl --list --verbose 2. Export the distribution data. wsl --export <distribution-name> <any-file-name>.tar For example, distribution name is Ubuntu-18.04: wsl --export Ubuntu-18.04 exported-ubuntu-18-04.tar 3. Unregister the distribution. wsl --unregister Ubun