Scripts2026年4月12日·1 分钟阅读

Express — Fast Unopinionated Minimalist Web Framework for Node.js

Express is the original, most popular web framework for Node.js. Minimal, flexible, and the foundation of countless APIs. The go-to starting point for Node.js backends that inspired Koa, Hono, Fastify, and many others.

SC
Script Depot · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

mkdir my-api && cd my-api
npm init -y
npm i express
// server.js
import express from "express";
const app = express();

app.use(express.json());

const assets = [
  { id: 1, repo: "react", stars: 230000 },
  { id: 2, repo: "vue",   stars: 210000 },
];

app.get("/api/assets", (req, res) => {
  res.json(assets);
});

app.get("/api/assets/:id", (req, res) => {
  const asset = assets.find(a => a.id === +req.params.id);
  if (!asset) return res.status(404).json({ error: "Not found" });
  res.json(asset);
});

app.post("/api/assets", (req, res) => {
  const asset = { id: assets.length + 1, ...req.body };
  assets.push(asset);
  res.status(201).json(asset);
});

app.listen(3000, () => console.log("Listening on :3000"));
node server.js
curl http://localhost:3000/api/assets
介绍

Express is the fast, unopinionated, minimalist web framework for Node.js. Created by TJ Holowaychuk in 2010, Express became the de facto standard for building web applications and APIs in Node.js. It provides a thin layer of fundamental web application features without obscuring Node.js features. Nearly every Node.js tutorial starts with Express.

What Express Does

  • Routing — path, method, regex matching
  • Middleware — composable request/response pipeline
  • Request/response — enhanced req/res objects
  • Error handling — centralized error middleware
  • Static filesexpress.static() built in
  • Template engines — Pug, EJS, Handlebars via app.engine()
  • JSON parsingexpress.json() middleware
  • Cookies/sessions — via cookie-parser and express-session
  • CORS — via cors middleware
  • Express 5 — native async error handling (upcoming)

Architecture

Middleware stack: each app.use() adds a function to the pipeline. Request enters, flows through middleware in order, and hits the matching route handler. Router is a mini-app with its own middleware and routes. Error middleware has 4 arguments (err, req, res, next).

Self-Hosting

# Production
NODE_ENV=production node server.js

# PM2
npm i -g pm2
pm2 start server.js -i max
pm2 startup && pm2 save

# Docker
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
CMD ["node", "server.js"]

Key Features

  • Minimal and unopinionated
  • Middleware-based architecture
  • Huge middleware ecosystem
  • Template engine support
  • Static file serving
  • HTTP utility methods
  • Easy to learn
  • Express Generator for scaffolding
  • Works with any Node.js version

Comparison

Framework Speed Opinionated TS
Express OK No via @types
Fastify Fast Slightly Yes
Hono Fast No Yes
Koa OK No via @types
NestJS OK (Express/Fastify) Yes Yes

常见问题 FAQ

Q: Express 过时了吗? A: 没有。Fastify、Hono 性能更好,但 Express 生态最大、教程最多、中间件最丰富。很多生产应用还在用而且很稳定。Express 5 正在开发中。

Q: TypeScript 支持? A: 用 @types/express 有完整类型。很多项目用 ts-node 或 tsx 开发。

Q: 错误处理? A: Express 4 需要 try-catch 包装 async handler 或用 express-async-errors。Express 5 原生支持 async 抛错自动进入 error middleware。

来源与致谢 Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产