npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@redhare/middlewares

v0.0.2

Published

NestJS provide a Middleware concept. Middleware is a function which is called before the route handler. Middleware functions have access to the request and response objects, and the next() middleware function in the application’s request-response cycle. T

Downloads

4

Readme

@infra-node-kit/middlewares

NestJS provide a Middleware concept. Middleware is a function which is called before the route handler. Middleware functions have access to the request and response objects, and the next() middleware function in the application’s request-response cycle. This package function is to collect commonly used middwares.

Installation

yarn add '@infra-node-kit/middlewares'

AccessLogMiddleware

Introduce

This middleware is to log the request and response information, user can also custom the log method.

Usage

import { AccessLogMiddleware } from '@infra-node-kit/middlewares'

@Module({
  providers: [
    {
      provide: 'INFRA_NODE_LOG',
      useValue: logger,
    },
  ],
  controllers: [AppController],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(AccessLogMiddleware).forRoutes('*')
  }
}

Custom Option

GetAccessLogMiddleware( options: IAccessLogOptions )

interface IAccessLogOptions {
  skipPaths?: string[] // the path in the skipPaths will ignore log access
  skipHeadersPaths?: string[]  // the path in the skipHeadersPaths will ignore log request headers
  skipBodyPaths?: string[] // the path in the skipBodyPaths will ignore log request body
}

The path rule can reference to path-to-regexp

Besides path-to-regexp rules you can use * to match all the paths.

import { GetAccessLogMiddleware } from '@infra-node-kit/middlewares'

@Module({
  providers: [
    {
      provide: 'INFRA_NODE_LOG',
      useValue: logger,
    },
  ],
  controllers: [AppController],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(GetAccessLogMiddleware({
      skipPaths: ['/_health']
    })).forRoutes('*')
  }
}

Log fields

Request start log fields:

// if request is GET http://localhost:8080/api/controller?a=123
export interface IAccessLogFields {
  method: string // GET toUpperCase
  uri: string // /api/controller?a=123
  protocol: string // http
  host: string
  path: string // /api/controller
  ip: string
  userAgent: string
  action: MIDDLEWARE_ACTION // REQUEAT_START
  headers?: string
  body?: string
}

export enum MIDDLEWARE_ACTION {
  REQUEAT_START = 'REQUEAT_START',
  REQUEAT_END = 'REQUEAT_END',
}

Request end log fields:

export interface IAccessLogFieldsEnd extends IAccessLogFields {
  status: number // response http status code
  contentLength: number // response content length
  cost: number  // request cost time
}

SetupLogger

AccessLogMiddleware inner logger instance use the token inject, the token name is 'INFRA_NODE_LOG', this instance required info method. So you need set a global provide named 'INFRA_NODE_LOG' to match. It is strongly recommended that you use the @infra-node-kit/logger module.

CustomLogger

if you want custom logger, you logger instance info method will receive a object which contains fileds in the previous section.

HealthCheckMiddleware

Introduce

Sometimes node services need to provoid some urls just to check the service status. This middleware is used to set these Health CheckURLs. The reason for using middleware is that you need to put the Health CheckURLs in the outermost layer before any logic.

Usage

import { HealthCheckMiddleware,defaultHealthCheckPaths } from '@infra-node-kit/middlewares'

@Module({
  controllers: [AppController],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(HealthCheckMiddleware).forRoutes(...defaultHealthCheckPaths)
  }
}

defaultValue

export const defaultHealthCheckPaths = ['/__health_check']
export const defaultHealthCheckResponse = {
  code: 0,
  msg: 'OK',
  status: 200,
}

CustomPathsAndResponse

import { GetHealthCheckMiddleware } from '@infra-node-kit/middlewares'

@Module({
  controllers: [AppController],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    const customHealthPaths = ['/__custom_health_check']
    consumer
      .apply(
        GetHealthCheckMiddleware({
          paths: customHealthPaths,
          response: { code: 1 },
        }),
      )
      .forRoutes(...customHealthPaths)
  }
}