express
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseExpress
Express
Express is the standard web framework for Node.js. Express 5 (2025) finally stabilizes modern features like Promise support in middleware, removing the need for .
express-async-errorsExpress是Node.js的标准Web框架。Express 5(2025版)终于稳定了诸如中间件中的Promise支持等现代特性,不再需要使用。
express-async-errorsWhen to Use
适用场景
- Microservices: Lightweight and fast.
- REST APIs: The standard for building JSON APIs in Node.
- Learning: The best way to learn how HTTP works in Node.
- 微服务:轻量且高效。
- REST API:Node.js中构建JSON API的标准方案。
- 学习:了解Node.js中HTTP工作原理的最佳途径。
Quick Start (Express 5)
快速开始(Express 5)
javascript
import express from "express";
const app = express();
// Express 5 handles async errors automatically!
app.get("/", async (req, res) => {
const user = await db.getUser(); // If this throws, Express catches it.
res.json(user);
});
app.listen(3000);javascript
import express from "express";
const app = express();
// Express 5会自动处理异步错误!
app.get("/", async (req, res) => {
const user = await db.getUser(); // 如果此处抛出错误,Express会自动捕获。
res.json(user);
});
app.listen(3000);Core Concepts
核心概念
Middleware
中间件
Functions that have access to , , and . They form a pipeline.
.
reqresnextLog -> Auth -> BodyParse -> RouteHandler能够访问、和的函数,它们构成一个处理管道,流程为:。
reqresnext日志 -> 认证 -> 解析请求体 -> 路由处理器Routing
路由
app.get()app.post()使用、,基于正则表达式的简单路由机制。
app.get()app.post()Best Practices (2025)
2025年最佳实践
Do:
- Upgrade to Express 5: Native Promise support is a game changer for cleaner code.
- Use : Security headers are mandatory.
helmet - Structure properly: Don't put everything in . Use
app.js.express.Router()
Don't:
- Don't stick to CommonJS: Express 5 supports ESM. Use .
import - Don't use separate package: Use
body-parser(included since v4, standard in v5).express.json()
建议做:
- 升级到Express 5:原生Promise支持能极大简化代码,提升整洁度。
- 使用:安全头是必备配置。
helmet - 合理架构:不要将所有代码都放在中,使用
app.js进行拆分。express.Router()
不建议做:
- 不要固守CommonJS:Express 5支持ESM,使用语法。
import - 不要单独使用包:使用
body-parser(自v4版本已内置,v5中为标准配置)。express.json()