Simple startup utilities for launching Egg.js applications in single-process mode, ideal for development and testing environments.
Main function for starting an Egg.js application in single process mode with both application and agent workers.
/**
* Start egg application with single process mode
* @param options - Startup configuration options
* @returns Promise resolving to SingleModeApplication instance
*/
async function startEgg(options?: StartEggOptions): Promise<SingleModeApplication>;
/**
* Alias for startEgg function
* @param options - Same as startEgg options
* @returns Promise resolving to SingleModeApplication instance
*/
const start: typeof startEgg;
interface StartEggOptions {
/** Specify framework that can be absolute path or npm package */
framework?: string;
/** Directory of application, default to process.cwd() */
baseDir?: string;
/** Ignore single process mode warning */
ignoreWarning?: boolean;
/** Process mode, always 'single' for this function */
mode?: 'single';
/** Environment name */
env?: string;
/** Plugin configuration override */
plugins?: EggPlugin;
}Usage Examples:
import { startEgg } from "egg";
// Basic startup
const app = await startEgg();
console.log('Application started on port 7001');
// Startup with custom options
const app = await startEgg({
baseDir: '/path/to/my/app',
env: 'development',
framework: 'egg',
ignoreWarning: true
});
// Access both app and agent
console.log('App ready:', app.ready);
console.log('Agent ready:', app.agent.ready);
// Graceful shutdown
process.on('SIGTERM', async () => {
await app.close();
});Type definitions for single-process mode applications that combine both application and agent functionality.
/**
* Application instance in single mode with agent reference
*/
interface SingleModeApplication extends Application {
/** Agent worker instance */
agent: SingleModeAgent;
}
/**
* Agent instance in single mode with application reference
*/
interface SingleModeAgent extends Agent {
/** Application worker instance */
app: SingleModeApplication;
}Usage Examples:
import { startEgg, type SingleModeApplication } from "egg";
const app: SingleModeApplication = await startEgg({
baseDir: process.cwd()
});
// Access application functionality
app.use(async (ctx, next) => {
ctx.body = 'Hello Egg.js';
await next();
});
// Access agent functionality
app.agent.messenger.broadcast('custom-event', { data: 'test' });
// Both app and agent are cross-referenced
console.log(app.agent.app === app); // true
console.log(app.agent.application === app); // trueThe startup process automatically detects the framework configuration from package.json if not explicitly provided.
Usage Examples:
// package.json configuration
{
"name": "my-egg-app",
"egg": {
"framework": "my-custom-framework"
}
}
// Startup will automatically use the framework from package.json
const app = await startEgg({
baseDir: '/path/to/app'
// framework is auto-detected from package.json
});
// Or explicitly override
const app = await startEgg({
baseDir: '/path/to/app',
framework: 'egg' // Override package.json setting
});Single process applications handle the complete lifecycle of both workers.
Usage Examples:
import { startEgg } from "egg";
async function bootstrap() {
const app = await startEgg({
baseDir: process.cwd(),
env: process.env.NODE_ENV || 'development'
});
// Both agent and application are ready
console.log('Agent ready:', await app.agent.ready());
console.log('Application ready:', await app.ready());
// Listen for egg-ready event
app.messenger.on('egg-ready', () => {
console.log('All workers ready');
});
return app;
}
// Graceful shutdown handling
async function shutdown(app: SingleModeApplication) {
console.log('Shutting down...');
// Close application (will also close agent in single mode)
await app.close();
console.log('Shutdown complete');
process.exit(0);
}
// Usage
const app = await bootstrap();
process.on('SIGTERM', () => shutdown(app));
process.on('SIGINT', () => shutdown(app));type EggEnvType = 'local' | 'unittest' | 'prod' | string;
interface EggPluginItem {
env?: EggEnvType[];
path?: string;
package?: string;
enable?: boolean;
}
interface EggPlugin {
[key: string]: EggPluginItem | boolean | undefined;
onerror?: EggPluginItem;
session?: EggPluginItem;
i18n?: EggPluginItem;
watcher?: EggPluginItem;
multipart?: EggPluginItem;
security?: EggPluginItem;
development?: EggPluginItem;
logrotator?: EggPluginItem;
schedule?: EggPluginItem;
static?: EggPluginItem;
jsonp?: EggPluginItem;
view?: EggPluginItem;
}