@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
Keywords
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)
}
}