Skip to main content

Posts

Showing posts with the label http

HTTP Compression

When a server delivers some messages to a client using an HTTP protocol, data compression may be performed to save bandwidth. It can make the data size becomes smaller with the cost of CPU processes. Node.js has a built-in library to handle compression, as I mentioned in another post . For instance, we will see an HTTP server built on Fastify that utilizes the zlib module to compress data returned by the server module in the following code. import Fastify, { FastifyInstance } from 'fastify'; import { join } from 'path'; import { createReadStream } from 'fs'; import zlib from 'zlib'; const PORT = 3000; const fastify: FastifyInstance = Fastify({ logger: true }); fastify.get('/', (request, reply) => { // get request header const acceptEncoding = request.headers['accept-encoding'] || ''; const rawStream = createReadStream(join(process.cwd(), 'text.txt')); reply.header('Content-Type', 'text/plain...

Utilizing HTTP/2 Push for Faster Page Load in Node.js

HTTP/2 has several advantages over HTTP/1 that I've mention in my earlier post . In this post, I want to show how push-request can be performed using Node.js to create an HTTP/2 server. Push request is used to push static files such as scripts and styles so that the client can consume those static files as soon as possible without the need to request them first. In this example, several built-in Node modules are required and an external module for ease of content-type setting named mime . Let's install it first. npm init npm i --save mime HTTP/2 encodes all headers of a request and it presents several new headers for identifying a request such as :method and :path . For more clarity, I call some constants related to the HTTP/2 header from the http2.constants property. Let's create the server.js file. const http2 = require('http2'); const { HTTP2_HEADER_PATH, HTTP2_HEADER_METHOD, HTTP2_HEADER_CONTENT_TYPE, HTTP2_HEADER_CONTENT_LENGTH, HTTP2_...

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 the 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 adds a 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 a bidirectional flow of bytes that can contain one or more messages. Frame. It is the smallest unit of messages in the stream. It has a header that contains the identity to which the stream belongs. Frame can contain encoded header data or...