Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions src/content/docs/zh-cn/5x/starter/basic-routing.mdx
Original file line number Diff line number Diff line change
@@ -1,67 +1,65 @@
---
title: Basic routing
description: Learn the fundamentals of routing in Express.js applications, including how to define routes, handle HTTP methods, and create route handlers for your web server.
title: 基础路由
description: 学习 Express.js 应用中路由的基础知识,包括如何定义路由、处理 HTTP 方法以及为你的 Web 服务器创建路由处理函数。
---

import Alert from '@components/primitives/Alert/Alert.astro';

_Routing_ refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).
_路由(Routing)_ 指的是确定应用如何响应客户端对特定端点的请求,端点包含一个 URI(或路径)和特定的 HTTP 请求方法(GETPOST 等)。

Each route can have one or more handler functions, which are executed when the route is matched.
每个路由可以有一个或多个处理函数,当路由匹配时这些函数会被执行。

Route definition takes the following structure:
路由定义采用以下结构:

```js
app.METHOD(PATH, HANDLER);
```

Where:
其中:

- `app` is an instance of `express`.
- `METHOD` is an [HTTP request method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods), in lowercase.
- `PATH` is a path on the server.
- `HANDLER` is the function executed when the route is matched.
- `app` `express` 的一个实例。
- `METHOD` 是一个 [HTTP 请求方法](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods),使用小写。
- `PATH` 是服务器上的路径。
- `HANDLER` 是路由匹配时执行的函数。

<Alert type="alert">

This tutorial assumes that an instance of `express` named `app` is created and the server is
running. If you are not familiar with creating an app and starting it, see the Hello world
example.
本教程假设已创建一个名为 `app` 的 `express` 实例且服务器正在运行。如果你不熟悉如何创建和启动应用,请参阅 Hello World 示例。

</Alert>

The following examples illustrate defining simple routes.
以下示例演示了如何定义简单的路由。

Respond with `Hello World!` on the homepage:
在首页响应 `Hello World!`

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

Respond to a POST request on the root route (`/`), the application's home page:
在根路由(`/`),即应用首页,响应 POST 请求:

```js
app.post('/', (req, res) => {
res.send('Got a POST request');
});
```

Respond to a PUT request to the `/user` route:
响应 `/user` 路由的 PUT 请求:

```js
app.put('/user', (req, res) => {
res.send('Got a PUT request at /user');
});
```

Respond to a DELETE request to the `/user` route:
响应 `/user` 路由的 DELETE 请求:

```js
app.delete('/user', (req, res) => {
res.send('Got a DELETE request at /user');
});
```

For more details about routing, see the [routing guide](/guide/routing).
有关路由的更多详细信息,请参阅[路由指南](/guide/routing)
74 changes: 36 additions & 38 deletions src/content/docs/zh-cn/5x/starter/examples.mdx
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
---
title: Express examples
description: Explore a collection of Express.js application examples covering various use cases, integrations, and advanced configurations to help you learn and build your projects.
title: Express 示例
description: 探索一系列 Express.js 应用示例,涵盖各种用例、集成和高级配置,帮助你学习和构建项目。
---

import Alert from '@components/primitives/Alert/Alert.astro';

This page contains list of examples using Express.

- [auth](https://github.com/expressjs/express/tree/master/examples/auth) - Authentication with login and password
- [content-negotiation](https://github.com/expressjs/express/tree/master/examples/content-negotiation) - HTTP content negotiation
- [cookie-sessions](https://github.com/expressjs/express/tree/master/examples/cookie-sessions) - Working with cookie-based sessions
- [cookies](https://github.com/expressjs/express/tree/master/examples/cookies) - Working with cookies
- [downloads](https://github.com/expressjs/express/tree/master/examples/downloads) - Transferring files to client
- [ejs](https://github.com/expressjs/express/tree/master/examples/ejs) - Working with Embedded JavaScript templating (ejs)
- [error-pages](https://github.com/expressjs/express/tree/master/examples/error-pages) - Creating error pages
- [error](https://github.com/expressjs/express/tree/master/examples/error) - Working with error middleware
- [hello-world](https://github.com/expressjs/express/tree/master/examples/hello-world) - Simple request handler
- [markdown](https://github.com/expressjs/express/tree/master/examples/markdown) - Markdown as template engine
- [multi-router](https://github.com/expressjs/express/tree/master/examples/multi-router) - Working with multiple Express routers
- [mvc](https://github.com/expressjs/express/tree/master/examples/mvc) - MVC-style controllers
- [online](https://github.com/expressjs/express/tree/master/examples/online) - Tracking online user activity with `online` and `redis` packages
- [params](https://github.com/expressjs/express/tree/master/examples/params) - Working with route parameters
- [resource](https://github.com/expressjs/express/tree/master/examples/resource) - Multiple HTTP operations on the same resource
- [route-map](https://github.com/expressjs/express/tree/master/examples/route-map) - Organizing routes using a map
- [route-middleware](https://github.com/expressjs/express/tree/master/examples/route-middleware) - Working with route middleware
- [route-separation](https://github.com/expressjs/express/tree/master/examples/route-separation) - Organizing routes per each resource
- [search](https://github.com/expressjs/express/tree/master/examples/search) - Search API
- [session](https://github.com/expressjs/express/tree/master/examples/session) - User sessions
- [static-files](https://github.com/expressjs/express/tree/master/examples/static-files) - Serving static files
- [vhost](https://github.com/expressjs/express/tree/master/examples/vhost) - Working with virtual hosts
- [view-constructor](https://github.com/expressjs/express/tree/master/examples/view-constructor) - Rendering views dynamically
- [view-locals](https://github.com/expressjs/express/tree/master/examples/view-locals) - Saving data in request object between middleware calls
- [web-service](https://github.com/expressjs/express/tree/master/examples/web-service) - Simple API service

## Additional examples

These are some additional examples with more extensive integrations.
本页面包含使用 Express 的示例列表。

- [auth](https://github.com/expressjs/express/tree/master/examples/auth) - 使用用户名和密码进行身份验证
- [content-negotiation](https://github.com/expressjs/express/tree/master/examples/content-negotiation) - HTTP 内容协商
- [cookie-sessions](https://github.com/expressjs/express/tree/master/examples/cookie-sessions) - 使用基于 cookie 的会话
- [cookies](https://github.com/expressjs/express/tree/master/examples/cookies) - 使用 cookie
- [downloads](https://github.com/expressjs/express/tree/master/examples/downloads) - 向客户端传输文件
- [ejs](https://github.com/expressjs/express/tree/master/examples/ejs) - 使用 Embedded JavaScript 模板(ejs
- [error-pages](https://github.com/expressjs/express/tree/master/examples/error-pages) - 创建错误页面
- [error](https://github.com/expressjs/express/tree/master/examples/error) - 使用错误中间件
- [hello-world](https://github.com/expressjs/express/tree/master/examples/hello-world) - 简单的请求处理程序
- [markdown](https://github.com/expressjs/express/tree/master/examples/markdown) - 使用 Markdown 作为模板引擎
- [multi-router](https://github.com/expressjs/express/tree/master/examples/multi-router) - 使用多个 Express 路由器
- [mvc](https://github.com/expressjs/express/tree/master/examples/mvc) - MVC 风格的控制器
- [online](https://github.com/expressjs/express/tree/master/examples/online) - 使用 `online` `redis` 包跟踪在线用户活动
- [params](https://github.com/expressjs/express/tree/master/examples/params) - 使用路由参数
- [resource](https://github.com/expressjs/express/tree/master/examples/resource) - 对同一资源执行多个 HTTP 操作
- [route-map](https://github.com/expressjs/express/tree/master/examples/route-map) - 使用映射组织路由
- [route-middleware](https://github.com/expressjs/express/tree/master/examples/route-middleware) - 使用路由中间件
- [route-separation](https://github.com/expressjs/express/tree/master/examples/route-separation) - 为每个资源组织路由
- [search](https://github.com/expressjs/express/tree/master/examples/search) - 搜索 API
- [session](https://github.com/expressjs/express/tree/master/examples/session) - 用户会话
- [static-files](https://github.com/expressjs/express/tree/master/examples/static-files) - 提供静态文件
- [vhost](https://github.com/expressjs/express/tree/master/examples/vhost) - 使用虚拟主机
- [view-constructor](https://github.com/expressjs/express/tree/master/examples/view-constructor) - 动态渲染视图
- [view-locals](https://github.com/expressjs/express/tree/master/examples/view-locals) - 在中间件调用之间保存数据到请求对象
- [web-service](https://github.com/expressjs/express/tree/master/examples/web-service) - 简单的 API 服务

## 其他示例

以下是一些具有更广泛集成的其他示例。

<Alert type="warning">

This information refers to third-party sites, products, or modules that are not maintained by the
Expressjs team. Listing here does not constitute an endorsement or recommendation from the
Expressjs project team.
本信息引用的第三方网站、产品或模块不由 Expressjs 团队维护。此处列出并不代表 Expressjs 项目团队的认可或推荐。

</Alert>

- [prisma-fullstack](https://github.com/prisma/prisma-examples/tree/latest/pulse/fullstack-simple-chat) - Fullstack app with Express and Next.js using [Prisma](https://www.npmjs.com/package/prisma) as an ORM
- [prisma-rest-api-ts](https://github.com/prisma/prisma-examples/tree/latest/orm/express) - REST API with Express in TypeScript using [Prisma](https://www.npmjs.com/package/prisma) as an ORM
- [prisma-fullstack](https://github.com/prisma/prisma-examples/tree/latest/pulse/fullstack-simple-chat) - 使用 Express Next.js 的全栈应用,使用 [Prisma](https://www.npmjs.com/package/prisma) 作为 ORM
- [prisma-rest-api-ts](https://github.com/prisma/prisma-examples/tree/latest/orm/express) - 使用 TypeScript 的 Express REST API,使用 [Prisma](https://www.npmjs.com/package/prisma) 作为 ORM
82 changes: 31 additions & 51 deletions src/content/docs/zh-cn/5x/starter/faq.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,55 @@
---
title: FAQ
description: Find answers to frequently asked questions about Express.js, including topics on application structure, models, authentication, template engines, error handling, and more.
title: 常见问题
description: 查找有关 Express.js 的常见问题解答,包括应用结构、模型、身份验证、模板引擎、错误处理等主题。
---

## How should I structure my application?
## 我应该如何组织我的应用结构?

There is no definitive answer to this question. The answer depends
on the scale of your application and the team that is involved. To be as
flexible as possible, Express makes no assumptions in terms of structure.
这个问题没有绝对的答案。答案取决于你的应用规模以及参与的团队。为了尽可能灵活,Express 在结构方面不做任何假设。

Routes and other application-specific logic can live in as many files
as you wish, in any directory structure you prefer. View the following
examples for inspiration:
路由和其他应用特定的逻辑可以放在任意数量的文件中,使用你喜欢的任何目录结构。查看以下示例获取灵感:

- [Route listings](https://github.com/expressjs/express/blob/4.13.1/examples/route-separation/index.js#L32-L47)
- [Route map](https://github.com/expressjs/express/blob/4.13.1/examples/route-map/index.js#L52-L66)
- [MVC style controllers](https://github.com/expressjs/express/tree/master/examples/mvc)
- [路由列表](https://github.com/expressjs/express/blob/4.13.1/examples/route-separation/index.js#L32-L47)
- [路由映射](https://github.com/expressjs/express/blob/4.13.1/examples/route-map/index.js#L52-L66)
- [MVC 风格控制器](https://github.com/expressjs/express/tree/master/examples/mvc)

Also, there are third-party extensions for Express, which simplify some of these patterns:
此外,还有一些 Express 的第三方扩展,可以简化其中一些模式:

- [Resourceful routing](https://github.com/expressjs/express-resource)
- [资源路由](https://github.com/expressjs/express-resource)

## How do I define models?
## 我如何定义模型?

Express has no notion of a database. This concept is
left up to third-party Node modules, allowing you to
interface with nearly any database.
Express 没有数据库的概念。这个概念留给第三方 Node 模块,允许你与几乎任何数据库进行交互。

See [LoopBack](http://loopback.io) for an Express-based framework that is centered around models.
查看 [LoopBack](http://loopback.io),这是一个基于 Express 的以模型为中心的框架。

## How can I authenticate users?
## 我如何对用户进行身份验证?

Authentication is another opinionated area that Express does not
venture into. You may use any authentication scheme you wish.
For a simple username / password scheme, see [this example](https://github.com/expressjs/express/tree/master/examples/auth).
身份验证是另一个 Express 不涉及的有争议领域。你可以使用任何你想要的身份验证方案。
对于简单的用户名/密码方案,请参阅[此示例](https://github.com/expressjs/express/tree/master/examples/auth)。

## Which template engines does Express support?
## Express 支持哪些模板引擎?

Express supports any template engine that conforms with the `(path, locals, callback)` signature.
To normalize template engine interfaces and caching, see the
[consolidate.js](https://github.com/visionmedia/consolidate.js)
project for support. Unlisted template engines might still support the Express signature.
Express 支持任何符合 `(path, locals, callback)` 签名的模板引擎。
要统一模板引擎接口和缓存,请参阅 [consolidate.js](https://github.com/visionmedia/consolidate.js) 项目获取支持。未列出的模板引擎可能仍然支持 Express 签名。

For more information, see [Using template engines with Express](/guide/using-template-engines).
有关更多信息,请参阅[在 Express 中使用模板引擎](/guide/using-template-engines)

## How do I handle 404 responses?
## 我如何处理 404 响应?

In Express, 404 responses are not the result of an error, so
the error-handler middleware will not capture them. This behavior is
because a 404 response simply indicates the absence of additional work to do;
in other words, Express has executed all middleware functions and routes,
and found that none of them responded. All you need to
do is add a middleware function at the very bottom of the stack (below all other functions)
to handle a 404 response:
在 Express 中,404 响应不是错误的结果,因此错误处理中间件不会捕获它们。这种行为是因为 404 响应仅仅表示没有更多工作要做;换句话说,Express 已执行所有中间件函数和路由,发现它们都没有响应。你只需要在栈的最底部(所有其他函数之下)添加一个中间件函数来处理 404 响应:

```js
app.use((req, res, next) => {
res.status(404).send("Sorry can't find that!");
});
```

Add routes dynamically at runtime on an instance of `express.Router()`
so the routes are not superseded by a middleware function.
在 `express.Router()` 实例上在运行时动态添加路由,以便路由不会被中间件函数覆盖。

## How do I setup an error handler?
## 我如何设置错误处理程序?

You define error-handling middleware in the same way as other middleware,
except with four arguments instead of three; specifically with the signature `(err, req, res, next)`:
你定义错误处理中间件的方式与其他中间件相同,只是有四个参数而不是三个;具体签名为 `(err, req, res, next)`:

```js
app.use((err, req, res, next) => {
Expand All @@ -75,16 +58,13 @@ app.use((err, req, res, next) => {
});
```

For more information, see [Error handling](/guide/error-handling).
有关更多信息,请参阅[错误处理](/guide/error-handling)

## How do I render plain HTML?
## 我如何渲染纯 HTML

You don't! There's no need to "render" HTML with the `res.render()` function.
If you have a specific file, use the `res.sendFile()` function.
If you are serving many assets from a directory, use the `express.static()`
middleware function.
你不需要!没有必要使用 `res.render()` 函数来"渲染" HTML。如果你有一个特定的文件,使用 `res.sendFile()` 函数。如果你要从一个目录提供许多资产,使用 `express.static()` 中间件函数。

## What version of Node.js does Express require?
## Express 需要什么版本的 Node.js

- [Express 4.x](/4x/api) requires Node.js 0.10 or higher.
- [Express 5.x](/api) requires Node.js 18 or higher.
- [Express 4.x](/4x/api) 需要 Node.js 0.10 或更高版本。
- [Express 5.x](/api) 需要 Node.js 18 或更高版本。
Loading
Loading