How to Use Midway 3.x Info Component for Application Diagnostics
This guide explains how to install, configure, and use Midway 3.x's info component to expose application details, customize the info route, hide sensitive data, and access diagnostic APIs for deeper backend insight.
Info Component Overview
Midway 3.x provides an info component that displays basic application information, making troubleshooting easier.
Supported Scenarios
Standard projects ✅
Serverless ✅
Integrated projects ✅
Installation
$ npm i @midwayjs/info@3 --saveOr add the dependency to package.json and reinstall:
{
"dependencies": {
"@midwayjs/info": "^3.0.0",
// ...
}
}Using the Component
Import and configure the component in your code:
import { Configuration } from '@midwayjs/decorator';
import * as koa from '@midwayjs/koa';
import * as info from '@midwayjs/info';
@Configuration({
imports: [
koa,
info
]
})
export class AutoConfiguration {
// ...
}To enable the component only in specific environments, use enabledEnvironment:
import { Configuration } from '@midwayjs/decorator';
import * as koa from '@midwayjs/koa';
import * as info from '@midwayjs/info';
@Configuration({
imports: [
koa,
{
component: info,
enabledEnvironment: ['local'] // only enabled locally
}
]
})
export class AutoConfiguration {
// ...
}Viewing Information
By default, the info component adds a middleware for HTTP scenarios; you can access the data via /_info. The response includes system, process, and configuration details.
Example output:
Customizing the Access Route
For security, you can change the default route:
// src/config/config.default.ts
export default {
// ...
info: {
infoPath: '/_my_info',
}
}Hiding Sensitive Information
The component hides secret keys by default. You can extend the hidden keywords (affects environment variables and multi‑environment configs) using wildcards:
// src/config/config.default.ts
import { DefaultHiddenKey } from '@midwayjs/info';
export default {
// ...
info: {
hiddenKey: DefaultHiddenKey.concat(['*abc', '*def', '*bbb*']),
}
}Calling the API Directly
The component provides an InfoService for non‑HTTP or custom scenarios. Example usage:
import { Provide, Inject } from '@midwayjs/decorator';
import { InfoService } from '@midwayjs/info';
@Provide()
export class UserService {
@Inject()
infoService: InfoService;
async getInfo() {
this.infoService.projectInfo(); // application name, etc.
this.infoService.systemInfo(); // system details
this.infoService.resourceOccupationInfo(); // memory, CPU
this.infoService.softwareInfo(); // Midway framework info
this.infoService.midwayConfig(); // current config
this.infoService.midwayService(); // DI container services
this.infoService.timeInfo(); // time, timezone, uptime
this.infoService.envInfo(); // environment variables
this.infoService.dependenciesInfo(); // dependencies
this.infoService.networkInfo(); // network info
}
}Node Underground
No language is immortal—Node.js isn’t either—but thoughtful reflection is priceless. This underground community for Node.js enthusiasts was started by Taobao’s Front‑End Team (FED) to share our original insights and viewpoints from working with Node.js. Follow us. BTW, we’re hiring.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
