Moshangl Technical Documentation

website·Indexed 29 days ago

https://moshangl.cn

A collection of technical guides and configuration tutorials covering Crontab syntax, Docker deployment (including MinIO and OnlyOffice), MySQL installation, NestJS integration with Prisma, Redis, Swagger, and Winston logging, and Nginx setup.

Tokens
14.6K
Snippets
83
Records
95
Agent score
49%

What's inside Moshangl

  1. Overview of NestJS Framework

    Nest (NestJS) is a progressive Node.js framework for building efficient and scalable server-side applications. It is built with and fully supports TypeScript, while still allowing pure JavaScript. It combines elements of Object-Oriented Programming (OOP), Functional Programming (FP), and Functional Reactive Programming (FRP).

    Nest provides an abstraction layer over powerful HTTP server frameworks—Express by default, with the option to configure Fastify—while still exposing their underlying APIs to allow the use of third-party modules.

  2. Introduction to NestJS Application Architecture

    NestJS provides an out-of-the-box application architecture designed to help developers and teams build applications that are highly testable, scalable, loosely coupled, and easy to maintain. This architectural approach is heavily inspired by Angular, aiming to solve the common problem of lack of standardized architecture in Node.js and server-side JavaScript development.
  3. Nginx Overview and Key Features

    Nginx is a high-performance open-source Web server and reverse proxy designed to handle high concurrency and solve the C10K problem. Key technical characteristics include:

    • Architecture: Uses an event-driven asynchronous architecture for efficient concurrent connection handling.
    • Reverse Proxy & Load Balancing: Supports load balancing algorithms, caching, and SSL termination.
    • Reliability: Employs a multi-process model supporting hot deployment for high availability.
    • Design: Lightweight with low resource consumption and a modular design for extensibility.
  4. Create a symbolic link for global Nginx command access

    To start or manage Nginx from any directory without navigating to the sbin folder, create a symbolic link from the Nginx binary to a directory in your system's PATH (such as /usr/local/sbin).
    sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx
  5. Bootstrap a NestJS project from a starter repository

    Alternatively, you can clone a starter project from GitHub. For a TypeScript project, use typescript-starter.git. For a JavaScript project, use javascript-starter.git.
    git clone https://github.com/nestjs/typescript-starter.git project
    cd project
    npm install
    npm run start
  6. Enable remote access for the MySQL root user

    To allow the root user to connect from any host, update the host field for the root user in the mysql.user table to % and flush privileges.
    # Log in to MySQL
    sudo mysql -u root -p
    
    # Select the mysql system database
    use mysql;
    
    # Update root user host to allow any remote connection
    update user set host='%' where user='root';
    
    # Flush privileges
    FLUSH PRIVILEGES;
  7. Recommended directory structure for logging middleware

    Organize logging functionality by separating the middleware logic from the logger configuration. Place the logger configuration in a dedicated config directory and the middleware in a common middleware directory to ensure reusability across the application.
    src/
    ├── common/
    │   └── middleware/
    │       └── logger.middleware.ts   # Interface call log middleware
    ├── config/
    │   └── winston.config.ts       # Winston log configuration
    ├── app.module.ts
    └── main.ts
  8. Install PCRE2 from source

    Nginx requires the PCRE (Perl Compatible Regular Expressions) library to support regular expressions in configuration files.
    wget https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.44/pcre2-10.44.tar.gz
    sudo tar -zxvf pcre2-10.44.tar.gz
    cd pcre2-10.44
    sudo ./configure --prefix=/usr/local/pcre2
    sudo make
    sudo make install
  9. Create a global PrismaService in NestJS

    To integrate Prisma into a NestJS application, create a dedicated PrismaService that extends PrismaClient and a PrismaModule decorated with @Global(). This ensures the database service is available throughout the entire application without needing to import the module into every single feature module.

    The PrismaService should implement OnModuleInit to ensure the database connection is established when the module initializes.

    // src/prisma/prisma.service.ts
    import { Injectable, OnModuleInit } from "@nestjs/common";
    import { PrismaClient } from "@prisma/client";
    
    @Injectable()
    export class PrismaService extends PrismaClient implements OnModuleInit {
      constructor() {
        super({
          log: ["error", "warn", "info", "query"],
        });
      }
    
      async onModuleInit() {
        await this.$connect();
      }
    }
    
    // src/prisma/prisma.module.ts
    import { Global, Module } from "@nestjs/common";
    import { PrismaService } from "./prisma.service";
    
    @Global()
    @Module({
      providers: [PrismaService],
      exports: [PrismaService],
    })
    export class PrismaModule {}
  10. Update the MySQL root user password

    Update the password for the root user on the local host using the ALTER USER command and flush privileges to apply the changes.
    # Log in to MySQL
    sudo mysql -u root -p
    
    # Change the password
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
    
    # Flush privileges
    FLUSH PRIVILEGES;
  11. Initialize Prisma in a project

    Run the npx prisma init command to initialize Prisma. This action creates a new prisma directory containing a schema.prisma file, which is used to define your database schema and configuration.
    npx prisma init
  12. Configure VNC server for Ubuntu XFCE (24.04)

    For Ubuntu 24.04 using XFCE, configure the xstartup file to initialize Xresources and start the XFCE4 session. Note that VNC passwords are typically limited to 8 characters.
    # Start VNC and set password (max 8 chars)
    vncserver
    
    # Edit config file
    vi ~/.vnc/xstartup
    
    # Replace content with:
    #!/bin/sh
    xrdb "$HOME/.Xresources"
    xsetroot -solid grey
    export XKL_XMODMAP_DISABLE=1
    /etc/X11/Xsession
    startxfce4 &
    
    # Restart the desktop process
    vncserver -kill :1
    vncserver