Posts

Showing posts with the label validation

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...

Using TypeORM in a NestJS Project

Image
Introduction TypeORM is a powerful ORM (Object-Relational Mapper) library for the Node.js ecosystem, written in TypeScript. It allows developers to interact with databases using classes and objects instead of writing complex raw SQL queries. Key Advantages Excellent TypeScript support: Fully leverages decorators and type-checking, helping detect errors during development instead of at runtime. Supports multiple databases: Compatible with popular database management systems such as MySQL, PostgreSQL, SQLite, etc. Flexible patterns (Data Mapper & Active Record): Lets you choose the appropriate implementation style depending on project size (Data Mapper for large, complex projects; Active Record for smaller, quick projects). Migration management: Provides powerful tools to manage database schema changes systematically, helping synchronize changes across development teams. Query Builder: In addition to object-based operations, TypeORM provides a Query Builder for constructing complex q...

Enhance Security for NodeJS Application

Image
1. Limiting the number of requests Limiting the number of requests (from a single IP address within a specific timeframe) is a method used to prevent denial-of-service ( DOS, DDOS ) attacks or brute force attacks that could overload your server. If you're using Express , integrating this is quite straightforward using the express-rate-limit  package. import * as express from 'express' import helmet from 'helmet' import expressRateLimit from 'express-rate-limit' const app = express () const limiter = expressRateLimit ({ windowMs: 10 * 60 * 1000 , // ms, ~10 minutes max: 50 , // limit each IP to 50 requests }) const specificLimiter = expressRateLimit ({ windowMs: 60 * 60 * 1000 , // 1 hour window max: 2 , // start blocking after 2 requests message: 'Too many requests' , // default 429 TOO MANY REQUESTS }) app . use ( limiter ) // use for all route . use ( '/common' , ( req , res ) => { res . json ...