Nest.js如何改写简易版请求监控,实现更强大的功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1108个文字,预计阅读时间需要5分钟。
在Node中使用Nest.js实现请求监控,以下是一个简要的介绍和实现方法:
Nest.js是一个基于Node.js的框架,用于构建高效、可扩展的服务器端应用程序。它利用了TypeScript的优势,并提供了模块化、依赖注入和声明式路由等功能。
实现步骤:
1. 初始化Nest.js项目: bash nest new request-monitor cd request-monitor
2. 安装依赖: bash npm install @nestjs/common @nestjs/core @nestjs/platform-express @nestjs/websockets
3. 创建监控服务: 在`src/services`目录下创建`requestMonitor.service.ts`: typescript import { Injectable } from '@nestjs/common';
@Injectable() export class RequestMonitorService { private requests: { [key: string]: number }={};
trackRequest(path: string) { this.requests[path]=(this.requests[path] || 0) + 1; }
getRequests() { return this.requests; } }
4. 全局中间件: 在`src/middleware`目录下创建`requestMonitor.middleware.ts`: typescript import { Injectable, NestMiddleware } from '@nestjs/common'; import { Request, Response } from 'express'; import { RequestMonitorService } from '../services/requestMonitor.service';
@Injectable() export class RequestMonitorMiddleware implements NestMiddleware { constructor(private readonly requestMonitorService: RequestMonitorService) {}
use(req: Request, res: Response, next: Function) { const path=req.path; this.requestMonitorService.trackRequest(path); next(); } }
5. 配置中间件: 在`src/main.ts`中引入并使用中间件: typescript import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { RequestMonitorMiddleware } from './middleware/requestMonitor.middleware';
async function bootstrap() { const app=await NestFactory.create(AppModule); app.useGlobalMiddleware(RequestMonitorMiddleware); await app.listen(3000); }
bootstrap();
6. 查看监控数据: 在控制台或日志文件中查看`RequestMonitorService`中存储的请求数据。
通过以上步骤,您可以在Nest.js应用程序中实现一个简单的请求监控功能。希望这对您有所帮助!
node中怎么使用Nest.js 实现请求监控?下面本篇文章给大家介绍一下node 框架 Nest.js 实现简易版请求监控的方法,希望对大家有所帮助!平时我们做业务处理时,想看一个时间端的业务请求实况,看下某些接口 cpu 内存 等 使用情况,做出针对性的接口优化时要做一个监控系统。但是如果是自己搞一个小项目没有那么多资源应该如何实现呢。这里我们采用 Nest 的一个第三方包 nest-status-monitor 来看一下。
贴出 nodejs框架--Nest.js 中文文档以方便大家进一步学习 Nest 中文文档(docs.nestjs.cn/)
状态监控包文档 nest-status-monitor:
www.npmjs.com/package/nest-status-monitor
安装依赖
首先在我们的
Nest项目中安装一下需要的依赖yarn add nest-status-monitor状态监控包yarn add @nestjs/platform-socket.io 6.10.14需要用到的socket包 ,这里因为状态监控包由于版本和最新的socket包不兼容,故安装老的版本
实现
状态监控配置
/* statusMonitor.ts */ export default { pageTitle: 'Nest.js Monitoring ', // 配置端口 port: 3000, // 这里记得加全局路由 '/api' path: '/status', ignoreStartsWith: '/health/alive', spans: [ { interval: 1, // Every second retention: 60, // Keep 60 datapoints in memory }, { interval: 5, // Every 5 seconds retention: 60, }, { interval: 15, // Every 15 seconds retention: 60, }, ], chartVisibility: { cpu: true, mem: true, load: true, responseTime: true, rps: true, statusCodes: true, }, healthChecks: [], };
mian.ts 文件中引用,并注册
/* main.ts */ import { StatusMonitorModule } from 'nest-status-monitor'; import statusMonitorConfig from './config/statusMonitor'; async function bootstrap() { ... // 注册状态监控 StatusMonitorModule.setUp(statusMonitorConfig), } bootstrap();
效果
启动项目
yarn run start:dev在浏览器中输入 localhost:3000/api/status 这个是设置的显示地址。大家也可以根据自己需求环境去设置
如上图所示,我刚才测试发了两个
400的请求 与一个200的请求,清晰的展示在最下面。
总结
- 至此我们在开发成本不高的情况下已经弄好了一个小工具,可实时监控服务器情况。
- 大家知道那些支持 Nest 开源的小工具,可以在评论区进行交流。互相学习~( ̄▽ ̄)~*
更多node相关知识,请访问:nodejs 教程!
以上就是聊聊node中怎么使用Nest.js 实现简易版请求监控的详细内容,更多请关注自由互联其它相关文章!
本文共计1108个文字,预计阅读时间需要5分钟。
在Node中使用Nest.js实现请求监控,以下是一个简要的介绍和实现方法:
Nest.js是一个基于Node.js的框架,用于构建高效、可扩展的服务器端应用程序。它利用了TypeScript的优势,并提供了模块化、依赖注入和声明式路由等功能。
实现步骤:
1. 初始化Nest.js项目: bash nest new request-monitor cd request-monitor
2. 安装依赖: bash npm install @nestjs/common @nestjs/core @nestjs/platform-express @nestjs/websockets
3. 创建监控服务: 在`src/services`目录下创建`requestMonitor.service.ts`: typescript import { Injectable } from '@nestjs/common';
@Injectable() export class RequestMonitorService { private requests: { [key: string]: number }={};
trackRequest(path: string) { this.requests[path]=(this.requests[path] || 0) + 1; }
getRequests() { return this.requests; } }
4. 全局中间件: 在`src/middleware`目录下创建`requestMonitor.middleware.ts`: typescript import { Injectable, NestMiddleware } from '@nestjs/common'; import { Request, Response } from 'express'; import { RequestMonitorService } from '../services/requestMonitor.service';
@Injectable() export class RequestMonitorMiddleware implements NestMiddleware { constructor(private readonly requestMonitorService: RequestMonitorService) {}
use(req: Request, res: Response, next: Function) { const path=req.path; this.requestMonitorService.trackRequest(path); next(); } }
5. 配置中间件: 在`src/main.ts`中引入并使用中间件: typescript import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { RequestMonitorMiddleware } from './middleware/requestMonitor.middleware';
async function bootstrap() { const app=await NestFactory.create(AppModule); app.useGlobalMiddleware(RequestMonitorMiddleware); await app.listen(3000); }
bootstrap();
6. 查看监控数据: 在控制台或日志文件中查看`RequestMonitorService`中存储的请求数据。
通过以上步骤,您可以在Nest.js应用程序中实现一个简单的请求监控功能。希望这对您有所帮助!
node中怎么使用Nest.js 实现请求监控?下面本篇文章给大家介绍一下node 框架 Nest.js 实现简易版请求监控的方法,希望对大家有所帮助!平时我们做业务处理时,想看一个时间端的业务请求实况,看下某些接口 cpu 内存 等 使用情况,做出针对性的接口优化时要做一个监控系统。但是如果是自己搞一个小项目没有那么多资源应该如何实现呢。这里我们采用 Nest 的一个第三方包 nest-status-monitor 来看一下。
贴出 nodejs框架--Nest.js 中文文档以方便大家进一步学习 Nest 中文文档(docs.nestjs.cn/)
状态监控包文档 nest-status-monitor:
www.npmjs.com/package/nest-status-monitor
安装依赖
首先在我们的
Nest项目中安装一下需要的依赖yarn add nest-status-monitor状态监控包yarn add @nestjs/platform-socket.io 6.10.14需要用到的socket包 ,这里因为状态监控包由于版本和最新的socket包不兼容,故安装老的版本
实现
状态监控配置
/* statusMonitor.ts */ export default { pageTitle: 'Nest.js Monitoring ', // 配置端口 port: 3000, // 这里记得加全局路由 '/api' path: '/status', ignoreStartsWith: '/health/alive', spans: [ { interval: 1, // Every second retention: 60, // Keep 60 datapoints in memory }, { interval: 5, // Every 5 seconds retention: 60, }, { interval: 15, // Every 15 seconds retention: 60, }, ], chartVisibility: { cpu: true, mem: true, load: true, responseTime: true, rps: true, statusCodes: true, }, healthChecks: [], };
mian.ts 文件中引用,并注册
/* main.ts */ import { StatusMonitorModule } from 'nest-status-monitor'; import statusMonitorConfig from './config/statusMonitor'; async function bootstrap() { ... // 注册状态监控 StatusMonitorModule.setUp(statusMonitorConfig), } bootstrap();
效果
启动项目
yarn run start:dev在浏览器中输入 localhost:3000/api/status 这个是设置的显示地址。大家也可以根据自己需求环境去设置
如上图所示,我刚才测试发了两个
400的请求 与一个200的请求,清晰的展示在最下面。
总结
- 至此我们在开发成本不高的情况下已经弄好了一个小工具,可实时监控服务器情况。
- 大家知道那些支持 Nest 开源的小工具,可以在评论区进行交流。互相学习~( ̄▽ ̄)~*
更多node相关知识,请访问:nodejs 教程!
以上就是聊聊node中怎么使用Nest.js 实现简易版请求监控的详细内容,更多请关注自由互联其它相关文章!

