Top Related Projects
A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
Expressive middleware for node.js using ES2017 async functions
Fast, unopinionated, minimalist web framework for node.
Fast and low overhead web framework, for Node.js
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
- 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;
- 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;
- 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
- Install Egg.js:
$ npm init egg --type=simple
$ cd example-app
$ npm i
- Start the development server:
$ npm run dev
$ open http://localhost:7001
- 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;
- Configure routing:
// app/router.js
module.exports = app => {
const { router, controller } = app;
router.get('/', controller.home.index);
};
Competitor Comparisons
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.
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.
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.
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.
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 designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
English | ç®ä½ä¸æ
Features
- Built-in Process Management
- Plugin System
- Framework Customization
- Lots of plugins
Quickstart
Follow the commands listed below.
$ mkdir showcase && cd showcase
$ npm init egg --type=simple # Optionally pnpm create egg --type=simple
$ npm install
$ npm run dev
$ open http://localhost:7001
Node.js >= 14.20.0 required.
Documentations
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.
Sponsors and Backers
License
Top Related Projects
A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
Expressive middleware for node.js using ES2017 async functions
Fast, unopinionated, minimalist web framework for node.
Fast and low overhead web framework, for Node.js
The API and real-time application framework
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot