Posts

Showing posts with the label express

NestJS Controller and Swagger Guide

Image
Introduction NestJS is a progressive Node.js framework built with TypeScript, facilitating the development of efficient and scalable server-side applications. By default, NestJS uses Express as its core HTTP processing library. Key advantages include a tight modular architecture inspired by Angular and strong TypeScript support to reduce code errors. Controllers serve as the layer for processing incoming requests and returning responses to the client side. Their primary responsibility is to receive HTTP requests, route data to business logic services, and coordinate the returned result in the correct format. Swagger is a powerful suite of tools used for designing, building, and documenting RESTful APIs developed based on the OpenAPI data format specification. In NestJS, it helps automatically generate an intuitive UI interface for testing endpoints, enabling developers and stakeholders to understand the API structure without directly reading the source code. Detail After creating the N...

NestJS Practice Series

Image
Introduction NestJS is a progressive NodeJS framework designed for building efficient, reliable, and scalable server-side applications. Built on top of TypeScript (but also supporting pure JavaScript), NestJS combines elements of Object-Oriented Programming (OOP), Functional Programming (FP), and Functional Reactive Programming (FRP). The outstanding advantages of NestJS include: Modular architecture: Helps organize code scientifically, easy to maintain and expand for large projects. Powerful TypeScript support: Fully leverage static typing to minimize errors during development. Flexibility: Allows easy integration with other libraries (like Express or Fastify) and supports a wide variety of communication protocols (REST, GraphQL, WebSockets, Microservices). Rich ecosystem: Provides ready-to-use tools for processing common tasks like Validation, Caching, Database mapping (TypeORM/Prisma), and Authentication. Dependency Injection: A powerful mechanism that helps manage...

Create API Gateway with fast-gateway

Image
Introduction In this article, I will guide you on how to use fast-gateway to deploy a simple API Gateway on NodeJS along with express . The advantage of an API Gateway is that it acts as an intermediary layer to hide the rest of the system, including services, commonly used in Microservices architecture. Example Usage This is the Microservices model after deployment: First, install the package yarn add fast-gateway Next, define the ports that will be used. import * as express from 'express' import * as gateway from 'fast-gateway' const portGateway = 5000 const portService1 = 5001 const portService2 = 5002 Define service 1 as follows: const startService1 = (): void => { const app = express () app . get ( '/list' , ( req , res ) => { const items = [ { id: 1 , name: 'service 1 value 1' , }, { id: 2 , name: 'service 1 value 2' , }, ] re...

Creating API Documentation with Swagger on NodeJS

Image
Introduction Swagger is a popular, simple, and user-friendly tool for creating APIs . Most backend developers, regardless of the programming languages they use, are familiar with Swagger . This article will guide you through creating API documentation using Swagger on Node.js (specifically integrated with the Express framework). This is handy when you want to provide API documentation in a professional UI format for stakeholders involved in integration. Restful API REST stands for Representational State Transfer . It is an architectural style that defines a set of constraints for creating web services. RESTful APIs provide a simple and flexible way to access web services without complex processing. Common HTTP Methods in RESTful APIs : - GET : Used to read (retrieve) a representation of a resource. It returns data in XML or JSON format. - POST : Creates new resources or subordinates to existing ones. - PUT : Updates existing resources or creates new ones if the client chooses the...