midway
🍔 A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 🌈
Top Related Projects
A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
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
Midway is a Node.js framework developed by Alibaba, designed for building enterprise-grade applications. It combines the best practices of both object-oriented programming and functional programming, offering a flexible and scalable solution for developing web applications and microservices.
Pros
- Supports both TypeScript and JavaScript, providing type safety and better developer experience
- Offers a modular architecture, allowing for easy integration of various components and plugins
- Provides built-in dependency injection, making it easier to manage and test application components
- Includes support for both traditional MVC and serverless architectures
Cons
- Steeper learning curve compared to simpler Node.js frameworks like Express
- Documentation is primarily in Chinese, which may be challenging for non-Chinese speakers
- Smaller community compared to more established frameworks like Express or Nest.js
- Some advanced features may be overkill for smaller projects
Code Examples
- Creating a simple controller:
import { Controller, Get } from '@midwayjs/decorator';
@Controller('/')
export class HomeController {
@Get('/')
async home() {
return 'Hello Midway!';
}
}
- Using dependency injection:
import { Provide, Inject } from '@midwayjs/decorator';
@Provide()
export class UserService {
async getUser(id: number) {
// Fetch user logic
}
}
@Provide()
export class UserController {
@Inject()
userService: UserService;
async getUser(id: number) {
return this.userService.getUser(id);
}
}
- Creating a middleware:
import { Middleware } from '@midwayjs/decorator';
import { IMiddleware } from '@midwayjs/core';
@Middleware()
export class ReportMiddleware implements IMiddleware {
resolve() {
return async (ctx, next) => {
const startTime = Date.now();
await next();
console.log(`Request took ${Date.now() - startTime}ms`);
};
}
}
Getting Started
To start a new Midway project:
-
Install the Midway CLI:
npm install -g @midwayjs/cli
-
Create a new project:
midway-init my-midway-app
-
Navigate to the project directory and install dependencies:
cd my-midway-app npm install
-
Start the development server:
npm run dev
Your Midway application is now running at http://localhost:7001
.
Competitor Comparisons
A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
Pros of Nest
- Larger community and ecosystem, with more third-party packages and resources
- More comprehensive documentation and learning materials
- Better support for microservices architecture out of the box
Cons of Nest
- Steeper learning curve, especially for developers new to TypeScript or decorators
- Heavier framework with more boilerplate code
- Potentially slower startup time due to its dependency injection system
Code Comparison
Nest:
@Controller('cats')
export class CatsController {
@Get()
findAll(): string {
return 'This action returns all cats';
}
}
Midway:
@Provide()
@Controller('/cats')
export class CatsController {
@Get('/')
async findAll(): Promise<string> {
return 'This action returns all cats';
}
}
Both frameworks use decorators for defining controllers and routes, but Midway requires the @Provide()
decorator for dependency injection. Nest's approach is slightly more concise, while Midway's is more explicit about its DI system.
Nest and Midway are both TypeScript-based Node.js frameworks, but they cater to different preferences and use cases. Nest is more popular and feature-rich, while Midway offers a simpler learning curve and faster development for certain scenarios.
🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
Pros of Egg
- More mature and established ecosystem with a larger community
- Extensive plugin system for easy extensibility
- Strong focus on convention over configuration
Cons of Egg
- Primarily uses JavaScript, which may lack some TypeScript benefits
- Less flexible for non-traditional project structures
- Steeper learning curve for developers new to the framework
Code Comparison
Egg:
// app/controller/home.js
'use strict';
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
this.ctx.body = 'Hello world';
}
}
module.exports = HomeController;
Midway:
// src/controller/home.ts
import { Controller, Get } from '@midwayjs/decorator';
@Controller('/')
export class HomeController {
@Get('/')
async home(): Promise<string> {
return 'Hello world';
}
}
Midway offers a more modern, TypeScript-first approach with decorators, while Egg follows a more traditional JavaScript class-based structure. Midway's syntax is more concise and leverages TypeScript features, potentially leading to improved developer experience and type safety. However, Egg's approach may be more familiar to developers coming from other Node.js frameworks.
Expressive middleware for node.js using ES2017 async functions
Pros of Koa
- Lightweight and minimalist framework, offering flexibility and customization
- Excellent performance due to its small footprint and efficient middleware system
- Large ecosystem with numerous plugins and middleware available
Cons of Koa
- Requires more setup and configuration for complex applications
- Less opinionated, which may lead to inconsistencies in large projects
- Steeper learning curve for beginners due to its minimalist nature
Code Comparison
Koa:
const Koa = require('koa');
const app = new Koa();
app.use(async ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
Midway:
import { Controller, Get, Provide } from '@midwayjs/decorator';
@Provide()
@Controller('/')
export class HomeController {
@Get('/')
async home() {
return 'Hello World';
}
}
Key Differences
- Midway uses TypeScript and decorators for a more structured approach
- Koa relies on middleware chaining, while Midway uses a more traditional MVC pattern
- Midway provides built-in dependency injection and IoC container
- Koa offers more flexibility in application structure, while Midway enforces a specific architecture
Both frameworks have their strengths, with Koa excelling in simplicity and flexibility, and Midway providing a more opinionated and feature-rich environment for enterprise-level applications.
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
- Well-established community support and extensive documentation
Cons of Express
- Lacks built-in TypeScript support, requiring additional setup
- No built-in dependency injection or modular architecture
- Limited out-of-the-box features compared to more opinionated frameworks
Code Comparison
Express:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
Midway:
import { Controller, Get } from '@midwayjs/decorator';
@Controller('/')
export class HomeController {
@Get('/')
async home() {
return 'Hello World!';
}
}
Express offers a more straightforward approach with less boilerplate, while Midway provides a more structured, decorator-based setup with built-in TypeScript support. Midway's approach aligns with modern TypeScript practices and offers better type safety out of the box. Express, being more lightweight, allows for greater flexibility in structuring applications but requires additional setup for TypeScript integration and advanced features.
Fast and low overhead web framework, for Node.js
Pros of Fastify
- Extremely high performance, often outperforming other Node.js frameworks
- Extensive plugin ecosystem for easy extensibility
- Built-in support for JSON Schema validation
Cons of Fastify
- Steeper learning curve for developers new to the framework
- Less opinionated, requiring more setup for complex applications
Code Comparison
Fastify:
const fastify = require('fastify')()
fastify.get('/', async (request, reply) => {
return { hello: 'world' }
})
fastify.listen(3000)
Midway:
import { Controller, Get, Provide } from '@midwayjs/decorator';
@Provide()
@Controller('/')
export class HomeController {
@Get('/')
async home() {
return { hello: 'world' };
}
}
Key Differences
- Fastify focuses on high performance and low overhead
- Midway is built on top of Node.js frameworks like Express or Koa
- Fastify uses a plugin system for extensibility
- Midway leverages TypeScript and decorators for a more structured approach
- Fastify is more lightweight and flexible
- Midway provides a more comprehensive, full-stack solution
Both frameworks have their strengths, with Fastify excelling in performance and simplicity, while Midway offers a more structured, full-featured development experience.
The API and real-time application framework
Pros of Feathers
- More mature and established ecosystem with a larger community
- Supports real-time events and WebSocket out of the box
- Flexible and modular architecture allowing easy customization
Cons of Feathers
- Steeper learning curve for developers new to its concepts
- Less opinionated, which may lead to inconsistent code structures across projects
- Limited built-in support for GraphQL compared to Midway
Code Comparison
Feathers service creation:
const feathers = require('@feathersjs/feathers');
const app = feathers();
app.use('messages', {
async find() {
return [{ id: 1, text: 'Hello' }];
}
});
Midway controller creation:
import { Controller, Get } from '@midwayjs/decorator';
@Controller('/api')
export class HomeController {
@Get('/')
async home() {
return 'Hello Midway!';
}
}
Both frameworks offer straightforward ways to create services or controllers. Feathers uses a more functional approach, while Midway leverages decorators for a more declarative style. Feathers' simplicity in service creation contrasts with Midway's TypeScript-first approach, which provides stronger typing and better IDE support out of the box.
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
Midway - ä¸ä¸ªé¢åæªæ¥çäºç«¯ä¸ä½ Node.js æ¡æ¶
English | ç®ä½ä¸æ
èµæº
- 2022 å¤å£ mini ç´é¢ä¼
- 2022 å¬å£ç´é¢ä¼
- 3.x beta åè½é¢è§
- 2021 ç§å£ç´é¢ä¼
- 2021 å¤å£ç´é¢ä¼
- v2 示ä¾æç¨
- 2.0 åå¸ä¼åæ¾ã2.0 åå¸ä¼æç« ã
ç¹æ§
- ð å ¨åè½ï¼æ¯æ Web åºç¨/Serverless/FaaS/å¾®æå¡/å°ç¨åºå端çå¤ç§åºæ¯ï¼åºäºè£ 饰å¨åä¾èµæ³¨å ¥å¼åä¼ä¸çº§åºç¨
- ð¦ å端éæï¼å ¨æ°çäºç«¯ä¸ä½åºç¨ç åä½éªï¼é¶ API è°ç¨ï¼ä½¿ç¨ "React Hooks " é£æ ¼ä¸ä½ç å
- ð´ **跨平å°**ï¼æ¯æé¨ç½²è³æ®é Server æ Serverless/FaaS ç¯å¢
- ð¶ æ©å±ï¼ç»ä»¶åæ©å±è½åï¼å¦å¤æ¯æä½¿ç¨ Koa/Express/Egg.js çææ件
- ð 示ä¾: å®æ¹æä¾å¤ç§åºæ¯ç示ä¾ä»£ç ï¼æ¹ä¾¿å¼åè å¿«éä¸æ
- ð¡ TypeScript å ¨é¢æ¯æ
æè¿°
Midway æ¯ä¸ä¸ªéç¨äºæ建 Serverless æå¡ï¼ä¼ ç»åºç¨ãå¾®æå¡ï¼å°ç¨åºå端ç Node.js æ¡æ¶ã
Midway å¯ä»¥ä½¿ç¨ Koaï¼Express æ Egg.js ä½ä¸ºåºç¡ Web æ¡æ¶ãå®è¿æä¾äºç¬ç«ä½¿ç¨çåºæ¬è§£å³æ¹æ¡ï¼ä¾å¦ Socket.ioï¼GRPCï¼Dubbo.js å RabbitMQ çã
æ¤å¤ï¼Midway ä¹éç¨äºå端/å ¨æ å¼å人åç Node.js æ æå¡å¨æ¡æ¶ãæ建ä¸ä¸ä¸ªåå¹´çåºç¨ç¨åºãå¯å¨ AWSï¼é¿éäºï¼è ¾è®¯äºåä¼ ç» VM /容å¨ä¸è¿è¡ãä¸ React å Vue è½»æ¾éæã ð
Demo
使ç¨è£ 饰å¨å¼å Web åºç¨
import { Controller, Get, Provide } from '@midwayjs/decorator';
@Provide()
@Controller('/')
export class HomeController {
@Get('/')
async home() {
return `Welcome to midwayjs!`;
}
}
使ç¨å½æ°å¼åå ¨æ åºç¨
å端代ç src/apis/lambda/index.ts
import {
Api,
Get,
Query,
useContext,
} from '@midwayjs/hooks';
export default Api(
Get(),
Query<{
page: string;
limit: string;
}>(),
async () => {
const ctx = useContext();
return {
page: ctx.query.page,
limit: ctx.query.limit,
};
}
);
å端è°ç¨ src/page/index.tsx
import getArticles from './api';
const response = await getArticles({
query: { page: '0', limit: '10' },
});
console.log(response); // { page: '0', limit: '10' }
æå¨è°ç¨
fetch('/api/articles?page=0&limit=10')
.then((res) => res.json())
.then((res) => console.log(res)); // { page: '0', limit: '10' }
å¿«éä¸æ
$ npm -v
## éæ©æ¨¡ç
$ npm init midway
## è¿å
¥é¡¹ç®è·¯å¾
cd my_midway_app && npm run dev
ææ¡£å社åº
社åºä¼ç§ç¤ºä¾å±ç¤º
1ãCool-Admin - ä¸ä¸ªå¾é ·çåå°æé管çæ¡æ¶
- å®ç½ï¼https://cool-js.com/
VSC Plugin
çç
群éä¼æçå¿çæåï¼ä¹ä¼ææ°çæ¬åå¸æ¨éã
è´¡ç®
请åç¥æ们å¯ä»¥ä¸ºä½ åäºä»ä¹ï¼ä¸è¿å¨æ¤ä¹åï¼è¯·æ£æ¥ä¸ä¸æ¯å¦æ å·²ç»åå¨çBugæè æè§ã
å¦æä½ æ¯ä¸ä¸ªä»£ç è´¡ç®è ï¼è¯·åè代ç è´¡ç®è§èã
è°å¨ä½¿ç¨
ä½ ä¹æ³å Logo ï¼å¯ä»¥ç¹å» è¿é æ·»å ã
License
æ们ç代ç ä½¿ç¨ MIT åè®®ï¼è¯·æ¾å¿ä½¿ç¨ã
Top Related Projects
A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
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