Convert Figma logo to code with AI

eggjs logoegg

🥚🥚🥚🥚 Born to build better enterprise frameworks and apps with Node.js & Koa. https://307.run/eggcode

18,998
1,811
18,998
406

Top Related Projects

75,420

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀

35,706

Expressive middleware for node.js using ES2017 async functions

69,228

Fast, unopinionated, minimalist web framework for node.

36,194

Fast and low overhead web framework, for Node.js

15,258

The API and real-time application framework

Quick Overview

Egg.js is a Node.js web framework built on top of Koa.js, designed for building enterprise-grade applications. It provides a set of best practices, plugins, and tools to help developers create scalable and maintainable server-side applications with ease.

Pros

  • Highly extensible plugin system
  • Built-in security features and best practices
  • Strong TypeScript support
  • Comprehensive documentation and active community

Cons

  • Steeper learning curve compared to simpler frameworks
  • Opinionated structure may not suit all project types
  • Performance overhead due to its layered architecture
  • Limited ecosystem compared to more established frameworks like Express

Code Examples

  1. Basic controller example:
// app/controller/home.js
const { Controller } = require('egg');

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'Hello, Egg.js!';
  }
}

module.exports = HomeController;
  1. Using a service:
// app/service/user.js
const { Service } = require('egg');

class UserService extends Service {
  async find(uid) {
    const user = await this.ctx.db.query('select * from user where uid = ?', uid);
    return user;
  }
}

module.exports = UserService;

// app/controller/user.js
const { Controller } = require('egg');

class UserController extends Controller {
  async info() {
    const { ctx } = this;
    const userId = ctx.params.id;
    const user = await ctx.service.user.find(userId);
    ctx.body = user;
  }
}

module.exports = UserController;
  1. Middleware example:
// app/middleware/error_handler.js
module.exports = () => {
  return async function errorHandler(ctx, next) {
    try {
      await next();
    } catch (err) {
      ctx.app.emit('error', err, ctx);
      const status = err.status || 500;
      ctx.body = { error: err.message };
      ctx.status = status;
    }
  };
};

Getting Started

  1. Install Egg.js:
$ npm init egg --type=simple
$ cd example-app
$ npm i
  1. Start the development server:
$ npm run dev
$ open http://localhost:7001
  1. Add a new controller:
// app/controller/home.js
const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    this.ctx.body = 'Hello world';
  }
}

module.exports = HomeController;
  1. Configure routing:
// app/router.js
module.exports = app => {
  const { router, controller } = app;
  router.get('/', controller.home.index);
};

Competitor Comparisons

75,420

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀

Pros of Nest

  • Built with TypeScript, offering strong typing and better tooling support
  • Modular architecture with dependency injection, promoting cleaner and more maintainable code
  • Extensive ecosystem with built-in support for various technologies (GraphQL, WebSockets, etc.)

Cons of Nest

  • Steeper learning curve due to its complex architecture and TypeScript usage
  • Potentially higher overhead for simple applications compared to Egg's lightweight approach
  • Less focus on convention over configuration, requiring more explicit setup

Code Comparison

Nest:

@Controller('cats')
export class CatsController {
  @Get()
  findAll(): string {
    return 'This action returns all cats';
  }
}

Egg:

// app/controller/home.js
exports.index = async (ctx) => {
  ctx.body = 'hi, egg';
};

Nest uses decorators and TypeScript for defining controllers and routes, while Egg follows a more traditional JavaScript approach with exported functions. Nest's code is more declarative and leverages TypeScript features, whereas Egg's code is more straightforward and familiar to Node.js developers.

Both frameworks have their strengths, with Nest offering a more structured and scalable approach suitable for large applications, while Egg provides a simpler and more lightweight solution for rapid development of smaller to medium-sized projects.

35,706

Expressive middleware for node.js using ES2017 async functions

Pros of Koa

  • Lightweight and minimalist, offering more flexibility and control
  • Simpler learning curve for developers familiar with Express.js
  • Excellent performance due to its small footprint

Cons of Koa

  • Requires more setup and configuration for complex applications
  • Less opinionated, which can lead to inconsistencies in large projects
  • Smaller ecosystem compared to more established frameworks

Code Comparison

Koa:

const Koa = require('koa');
const app = new Koa();

app.use(async ctx => {
  ctx.body = 'Hello World';
});

app.listen(3000);

Egg:

// app/router.js
module.exports = app => {
  const { router, controller } = app;
  router.get('/', controller.home.index);
};

// app/controller/home.js
const Controller = require('egg').Controller;
class HomeController extends Controller {
  async index() {
    this.ctx.body = 'Hello World';
  }
}
module.exports = HomeController;

Egg provides a more structured approach with built-in conventions, while Koa offers a minimal foundation for developers to build upon. Egg is better suited for large-scale applications with its robust ecosystem and plugins, whereas Koa shines in scenarios where developers need more control and flexibility in their application architecture.

69,228

Fast, unopinionated, minimalist web framework for node.

Pros of Express

  • Lightweight and minimalist framework, offering flexibility and simplicity
  • Extensive ecosystem with a wide range of middleware and plugins
  • Large community support and extensive documentation

Cons of Express

  • Lacks built-in structure, requiring developers to make more architectural decisions
  • Less opinionated, which can lead to inconsistencies across projects
  • Fewer out-of-the-box features compared to Egg

Code Comparison

Express:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello World!');
});

Egg:

// app/router.js
module.exports = app => {
  const { router, controller } = app;
  router.get('/', controller.home.index);
};

// app/controller/home.js
class HomeController extends Controller {
  async index() {
    this.ctx.body = 'Hello World!';
  }
}

Express provides a more straightforward approach, while Egg enforces a structured layout with separate router and controller files. Egg's approach promotes better organization for larger applications, whereas Express offers more flexibility for smaller projects or quick prototypes.

36,194

Fast and low overhead web framework, for Node.js

Pros of Fastify

  • Higher performance and lower overhead compared to Egg
  • More flexible plugin system allowing easier customization
  • Simpler and more lightweight codebase

Cons of Fastify

  • Less opinionated structure, which may require more setup for large projects
  • Smaller ecosystem and fewer built-in features than Egg
  • Less focus on convention over configuration

Code Comparison

Egg:

// app/controller/home.js
exports.index = async ctx => {
  ctx.body = 'Hello World';
};

Fastify:

// server.js
fastify.get('/', async (request, reply) => {
  return 'Hello World'
})

Both frameworks offer simple ways to create routes and handle requests, but Fastify's approach is more concise and function-based, while Egg follows a more structured, class-based controller pattern.

Fastify excels in performance and flexibility, making it ideal for microservices and APIs. Its plugin system allows for easy extensibility, but it may require more initial setup for larger applications.

Egg, on the other hand, provides a more opinionated structure with built-in features like middleware, plugins, and a service layer. This can be beneficial for larger teams and projects that require a standardized approach, but it may feel overly complex for smaller applications.

Ultimately, the choice between Fastify and Egg depends on project requirements, team preferences, and the desired balance between performance, structure, and built-in features.

15,258

The API and real-time application framework

Pros of Feathers

  • More flexible and modular architecture, allowing for easier customization and plugin development
  • Better support for real-time applications with built-in WebSocket integration
  • Extensive ecosystem of plugins and adapters for various databases and services

Cons of Feathers

  • Steeper learning curve for developers new to its concepts and architecture
  • Less opinionated structure, which may lead to inconsistencies in large projects
  • Smaller community compared to Egg, potentially resulting in fewer resources and third-party integrations

Code Comparison

Egg (Controller):

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'Hello World';
  }
}

Feathers (Service):

class MessageService {
  async find(params) {
    return [];
  }
  async create(data, params) {
    return data;
  }
}

Both frameworks offer different approaches to building Node.js applications. Egg focuses on providing a more structured and opinionated framework, while Feathers emphasizes flexibility and real-time capabilities. The choice between the two depends on project requirements, team preferences, and the specific use case.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

English | 简体中文

NPM version NPM quality NPM download Node.js Version FOSSA Status

Continuous Integration Test coverage Known Vulnerabilities Open Collective backers and sponsors

Features

  • Built-in Process Management
  • Plugin System
  • Framework Customization
  • Lots of plugins

Quickstart

Follow the commands listed below.

$ mkdir showcase && cd showcase
$ pnpm create egg@beta
$ pnpm install
$ pnpm run dev
$ open http://localhost:7001

Node.js >= 20.19.0 required, supports require(esm) by default.

Monorepo Structure

This project is structured as a pnpm monorepo with the following packages:

  • packages/egg - Main Eggjs framework
  • examples/helloworld-commonjs - CommonJS example application
  • examples/helloworld-typescript - TypeScript example application
  • site - Documentation website

The monorepo uses pnpm catalog mode for centralized dependency management, ensuring consistent versions across all packages.

Development Commands

# Install dependencies for all packages
pnpm install

# Build all packages
pnpm run build

# Test all packages
pnpm run test

# Run specific package commands
pnpm --filter=egg run test
pnpm --filter=@examples/helloworld-typescript run dev
pnpm --filter=site run dev

Local External Services

Some DAL, ORM, Redis, and ecosystem benchmark paths need local MySQL and Redis services. Start the repository-aligned Docker services before running those tests on a clean machine:

utoo run dev:services:start

This starts MySQL 8 and Redis 7, matching the main CI service versions, and creates the databases used by local DAL/ORM/e2e fixtures: test, apple, banana, test_runtime_datasource, test_runtime_dao, test_dal_plugin, test_dal_standalone, cnpmcore, and cnpmcore_unittest.

Useful commands:

utoo run dev:services:status
utoo run dev:services:stop
utoo run dev:services:reset

The default host ports are 127.0.0.1:3306 for MySQL and 127.0.0.1:6379 for Redis. If either port is already used, the start command stops before changing containers. Keep using the existing service if it is compatible with CI, or stop it and run the command again. You can change Docker host ports with EGG_DEV_SERVICES_MYSQL_PORT and EGG_DEV_SERVICES_REDIS_PORT; however, the full DAL/ORM/Redis local test path still expects the default host ports.

Image overrides are available for compatibility checks:

EGG_DEV_SERVICES_MYSQL_IMAGE=mysql:5.7 utoo run dev:services:start
EGG_DEV_SERVICES_REDIS_IMAGE=redis:7 utoo run dev:services:start

Run utoo run dev:services:reset before switching MySQL image families, for example between MySQL 8 and MySQL 5.7, because MySQL data directories are not downgrade-compatible across major versions.

Current hard-coded service assumptions:

  • Redis plugin fixtures under plugins/redis/test/fixtures/apps/**/config.* use 127.0.0.1:6379; skipped Redis plugin tests become runnable when that port is available.
  • Session Redis fixtures under plugins/session/test/fixtures/redis-session/config/config.default.js use 127.0.0.1:6379.
  • DAL runtime tests in tegg/core/dal-runtime/test/DataSource.test.ts and tegg/core/dal-runtime/test/DAO.test.ts use local MySQL on port 3306.
  • DAL module fixtures in tegg/plugin/dal/test/fixtures/apps/dal-app/modules/dal/module.yml and tegg/standalone/standalone/test/fixtures/dal-*/module.yml use local MySQL on port 3306.
  • ORM fixtures in tegg/plugin/orm/test/fixtures/prepare.js and tegg/plugin/orm/test/fixtures/apps/orm-app/config/config.default.ts use local MySQL on port 3306.

Documentations

Contributors

contributors

How to Contribute

Please let us know how can we help. Do check out issues for bug reports or suggestions first.

To become a contributor, please follow our contributing guide, and review the repository guidelines for day-to-day development tips.

Sponsors and Backers

sponsors backers

License

MIT

FOSSA Status

NPM DownloadsLast 30 Days