Stop flying blind in your πππ¬πππ apps! π
One thing I always missed from the πππ«ππ―ππ₯/ππ²π¦ππ¨π§π² world was the Web Profiler.
Being able to see every database query, request duration, and cache hit in real-time is a game-changer for debugging. πͺ
I couldn't find a perfect equivalent for NestJS... ππ π° πππππ ππ. π
Introducing π§ππ¬ππ£π¬-π©π«π¨ππ’π₯ππ« β a lightweight, web-based dashboard for your NestJS backend.
β
Inspect HTTP request with hell of details about it.
β
Track executed DB queries with analyze (PostgreSQL, MySQL, MongoDB).
β
Monitor cache operations.
β
Listing all the entities.
β
Listing all available routes.
β
Listing all logs from the terminal.
β
Web UI built right into your app.
Installation
npm install nestjs-profiler
Peer Dependencies (Optional)
Install the dependencies relevant to your project:
# For PostgreSQL
npm install pg
# For MongoDB
npm install mongodb
# For MySQL
npm install mysql2
# For Cache Profiling
npm install @nestjs/cache-manager cache-manager
Configuration
Import ProfilerModule in your root AppModule.
import { Module } from '@nestjs/common';
import { ProfilerModule } from 'nestjs-profiler';
@Module({
imports: [
ProfilerModule.forRoot({
// Global enable/disable (default: true)
enabled: process.env.NODE_ENV !== 'production',
// PostgreSQL Profiling
collectQueries: true,
explain: {
enabled: true,
thresholdMs: 50, // Only explain queries taking > 50ms
analyze: false, // If true, runs EXPLAIN ANALYZE (execution!)
},
// Add pgDriver
pgDriver: pg,
// Add mysql2 driver for MySQL profiling
mysqlDriver: mysql2,
// Add mongodb driver for MongoDB profiling
mongoDriver: mongodb,
// MongoDB Profiling (default: true)
collectMongo: true,
// MySQL Profiling (default: true)
collectMysql: true,
// Cache Profiling (default: true)
collectCache: true,
// Log Profiling (default: true)
collectLogs: true,
// Storage backend (default: InMemory)
// You can implement custom storage by passing an object implementing ProfilerStorage
storage: 'memory',
}),
],
})
export class AppModule {}
Usage
1. Initialize Explorers (Optional but Recommended)
To enable the Entity Explorer and Route Explorer features in the dashboard, initialize the profiler in your main.ts file right after creating the application.
// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ProfilerModule } from 'nestjs-profiler';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// Initialize Profiler Explorers
ProfilerModule.initialize(app);
await app.listen(3000);
}
bootstrap();
2. Accessing the Dashboard
Once configured, start your application. The profiler automatically intercepts requests and queries.
Navigate to http://localhost:3000/__profiler (or your app's port).
3. JSON API
You can also retrieve profile data programmatically:
-
GET /__profiler/json- List recent requests -
GET /__profiler/:id/json- Get details for a specific request
NOTE: IMPORTANT
- The profiler is designed for development and debugging. Do not enable in production due to performance overhead and potential security risks.
- If you have a prefix for your API routes (e.g.,
/api), you have to adjust the global prefix like this:
app.setGlobalPrefix('api', {
exclude: [{ path: '__profiler/(.*)', method: RequestMethod.ALL }],
});
I'd love to hear your feedback! What's the one feature you wish NestJS had out of the box?
Npm package link: nestjs-profiler
Github: NestJS-Profiler-Repo
Top comments (0)