A web application framework for Node.js with enterprise-grade development capabilities through a plugin-based architecture and convention-over-configuration approach
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core application and agent classes for managing web application lifecycle and multi-process coordination in Egg.js.
Main application class that extends EggApplicationCore, designed for app worker processes that handle HTTP requests.
/**
* Singleton instance in App Worker, extends EggApplicationCore
* @param options - Configuration options for the application
*/
class Application extends EggApplicationCore {
constructor(options?: Omit<EggApplicationCoreOptions, 'type'>);
/** HTTP server instance, set after 'server' event */
server?: http.Server;
/** Reference to Helper class for utility functions */
Helper: typeof Helper;
/** Global view locals shared across all requests */
get locals(): Record<string, any>;
set locals(val: Record<string, any>);
/** Cookie signing keys derived from config.keys */
get keys(): string[];
/** Handle client errors on raw socket */
onClientError(err: any, socket: Socket): void;
/** Setup server configuration and event handlers */
onServer(server: http.Server): void;
/** Run async function in background without blocking request */
runInBackground(scope: (ctx: Context) => Promise<void>, req?: unknown): void;
/** Dump application configuration and router information to files */
dumpConfig(): void;
}Usage Examples:
import { Application } from "egg";
// Create application instance
const app = new Application({
baseDir: '/path/to/app',
mode: 'single'
});
// Set global locals for view rendering
app.locals = {
title: 'My Egg App',
version: '1.0.0'
};
// Run background task
app.runInBackground(async (ctx) => {
// Background processing logic
await processLargeDataset();
});
// Handle server setup
app.on('server', (server) => {
app.onServer(server);
console.log('Server configured');
});Agent worker class that extends EggApplicationCore, designed for background tasks and inter-process communication.
/**
* Singleton instance in Agent Worker, extends EggApplicationCore
* @param options - Configuration options for the agent
*/
class Agent extends EggApplicationCore {
constructor(options?: Omit<EggApplicationCoreOptions, 'type'>);
/** Close agent and cleanup resources */
close(): Promise<void>;
}Usage Examples:
import { Agent } from "egg";
// Create agent instance
const agent = new Agent({
baseDir: '/path/to/app',
mode: 'cluster'
});
// Agent automatically handles background tasks
await agent.ready();
// Cleanup when shutting down
process.on('SIGTERM', async () => {
await agent.close();
});Base functionality shared between Application and Agent instances.
/**
* Core application functionality based on EggCore
*/
class EggApplicationCore extends EggCore {
declare options: Required<EggApplicationCoreOptions>;
/** Base context class for controllers/services */
BaseContextClass: typeof BaseContextClass;
/** Alias for BaseContextClass */
Controller: typeof BaseContextClass;
Service: typeof BaseContextClass;
Subscription: typeof BaseContextClass;
/** HTTP client classes */
HttpClient: typeof HttpClient;
ContextHttpClient: typeof ContextHttpClient;
/** Inter-process messenger */
readonly messenger: IMessenger;
/** Agent reference (in application) or application reference (in agent) */
agent?: Agent;
application?: Application;
/** Component loader */
declare loader: EggApplicationLoader;
/** Make HTTP request using app's HTTP client */
curl<T = any>(url: HttpClientRequestURL, options?: HttpClientRequestOptions): Promise<HttpClientResponse<T>>;
/** Create new HTTP client instance */
createHttpClient(options?: HttpClientOptions): HttpClient;
/** Get HTTP client instance (lazy-loaded) */
get httpClient(): HttpClient;
/** Wrap client class with cluster leader/follower pattern */
clusterWrapper(clientClass: unknown, options?: object): any;
/** Get all logger instances */
get loggers(): EggLoggers;
/** Get logger by name */
getLogger(name: string): EggLogger;
/** Application logger */
get logger(): EggLogger;
/** Core framework logger */
get coreLogger(): EggLogger;
/** Create anonymous context for testing/background tasks */
createAnonymousContext(req?: any): Context;
/** Run function in anonymous context scope */
runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>, req?: unknown): Promise<void>;
/** Create request context from HTTP request/response */
createContext(req: IncomingMessage, res: ServerResponse): Context;
/** Get inspectable application information */
inspect(): any;
/** Get configuration object for dumping */
dumpConfigToObject(): { config: any; meta: any };
/** Get cluster wrapper function */
get cluster(): (clientClass: unknown, options?: object) => any;
}Usage Examples:
// Access loggers
app.logger.info('Application started');
app.coreLogger.debug('Debug information');
// Make HTTP requests
const response = await app.curl('https://api.example.com/data', {
method: 'GET',
dataType: 'json'
});
// Create anonymous context for background tasks
const ctx = app.createAnonymousContext({
query: { userId: '123' }
});
// Run function in context scope
await app.runInAnonymousContextScope(async (ctx) => {
const user = await ctx.service.user.findById(ctx.query.userId);
console.log('User:', user);
});
// Create cluster client
const apiClient = app.clusterWrapper(ApiClient, {
responseTimeout: 5000
});interface EggApplicationCoreOptions extends Omit<EggCoreOptions, 'baseDir'> {
/** Process mode: cluster or single */
mode?: 'cluster' | 'single';
/** Port for cluster communication */
clusterPort?: number;
/** Application base directory */
baseDir?: string;
}
interface ClientErrorResponse {
body: string | Buffer;
status: number;
headers: { [key: string]: string };
}Install with Tessl CLI
npx tessl i tessl/npm-egg