diff --git a/src/content/docs/zh-cn/5x/starter/basic-routing.mdx b/src/content/docs/zh-cn/5x/starter/basic-routing.mdx index f14af2a5de..379442126a 100644 --- a/src/content/docs/zh-cn/5x/starter/basic-routing.mdx +++ b/src/content/docs/zh-cn/5x/starter/basic-routing.mdx @@ -1,38 +1,36 @@ --- -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 请求方法(GET、POST 等)。 -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` 是路由匹配时执行的函数。 -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 示例。 -The following examples illustrate defining simple routes. +以下示例演示了如何定义简单的路由。 -Respond with `Hello World!` on the homepage: +在首页响应 `Hello World!`: ```js app.get('/', (req, res) => { @@ -40,7 +38,7 @@ app.get('/', (req, res) => { }); ``` -Respond to a POST request on the root route (`/`), the application's home page: +在根路由(`/`),即应用首页,响应 POST 请求: ```js app.post('/', (req, res) => { @@ -48,7 +46,7 @@ app.post('/', (req, res) => { }); ``` -Respond to a PUT request to the `/user` route: +响应 `/user` 路由的 PUT 请求: ```js app.put('/user', (req, res) => { @@ -56,7 +54,7 @@ app.put('/user', (req, res) => { }); ``` -Respond to a DELETE request to the `/user` route: +响应 `/user` 路由的 DELETE 请求: ```js app.delete('/user', (req, res) => { @@ -64,4 +62,4 @@ app.delete('/user', (req, res) => { }); ``` -For more details about routing, see the [routing guide](/guide/routing). +有关路由的更多详细信息,请参阅[路由指南](/guide/routing)。 diff --git a/src/content/docs/zh-cn/5x/starter/examples.mdx b/src/content/docs/zh-cn/5x/starter/examples.mdx index 69f410e908..b3cd2f27e0 100644 --- a/src/content/docs/zh-cn/5x/starter/examples.mdx +++ b/src/content/docs/zh-cn/5x/starter/examples.mdx @@ -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 服务 + +## 其他示例 + +以下是一些具有更广泛集成的其他示例。 -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 项目团队的认可或推荐。 -- [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 diff --git a/src/content/docs/zh-cn/5x/starter/faq.md b/src/content/docs/zh-cn/5x/starter/faq.md index e487cc3501..5f7533ec84 100644 --- a/src/content/docs/zh-cn/5x/starter/faq.md +++ b/src/content/docs/zh-cn/5x/starter/faq.md @@ -1,58 +1,43 @@ --- -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) => { @@ -60,13 +45,11 @@ app.use((req, res, next) => { }); ``` -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) => { @@ -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 或更高版本。 diff --git a/src/content/docs/zh-cn/5x/starter/generator.mdx b/src/content/docs/zh-cn/5x/starter/generator.mdx index 4f22665f43..350691c541 100644 --- a/src/content/docs/zh-cn/5x/starter/generator.mdx +++ b/src/content/docs/zh-cn/5x/starter/generator.mdx @@ -1,26 +1,26 @@ --- -title: Express application generator -description: Learn how to use the Express application generator tool to quickly create a skeleton for your Express.js applications, streamlining setup and configuration. +title: Express 应用生成器 +description: 学习如何使用 Express 应用生成器工具快速创建 Express.js 应用的脚手架,简化设置和配置流程。 --- import Alert from '@components/primitives/Alert/Alert.astro'; -Use the application generator tool, `express-generator`, to quickly create an application skeleton. +使用应用生成器工具 `express-generator` 可以快速创建应用脚手架。 -You can run the application generator with the `npx` command (available in Node.js 8.2.0). +你可以使用 `npx` 命令(Node.js 8.2.0 及以上版本可用)来运行应用生成器。 ```bash $ npx express-generator ``` -For earlier Node versions, install the application generator as a global npm package and then launch it: +对于更早的 Node 版本,请将应用生成器安装为全局 npm 包,然后启动它: ```bash $ npm install -g express-generator $ express ``` -Display the command options with the `-h` option: +使用 `-h` 选项显示命令选项: ```bash $ express -h @@ -42,7 +42,7 @@ $ express -h -f, --force force on non-empty directory ``` -For example, the following creates an Express app named _myapp_. The app will be created in a folder named _myapp_ in the current working directory and the view engine will be set to Pug: +例如,以下命令创建一个名为 _myapp_ 的 Express 应用。该应用将在当前工作目录下名为 _myapp_ 的文件夹中创建,并将视图引擎设置为 Pug: ```bash $ express --view=pug myapp @@ -66,34 +66,34 @@ $ express --view=pug myapp create : myapp/bin/www ``` -Then install dependencies: +然后安装依赖: ```bash $ cd myapp $ npm install ``` -On MacOS or Linux, run the app with this command: +在 MacOS 或 Linux 上,使用以下命令运行应用: ```bash $ DEBUG=myapp:* npm start ``` -On Windows Command Prompt, use this command: +在 Windows 命令提示符上,使用以下命令: ```bash > set DEBUG=myapp:* & npm start ``` -On Windows PowerShell, use this command: +在 Windows PowerShell 上,使用以下命令: ```bash PS> $env:DEBUG='myapp:*'; npm start ``` -Then, load `http://localhost:3000/` in your browser to access the app. +然后,在浏览器中加载 `http://localhost:3000/` 访问应用。 -The generated app has the following directory structure: +生成的应用具有以下目录结构: ```bash . @@ -119,7 +119,7 @@ The generated app has the following directory structure: -The app structure created by the generator is just one of many ways to structure Express apps. -Feel free to use this structure or modify it to best suit your needs. +生成器创建的应用结构只是构建 Express 应用的众多方式之一。 +你可以自由使用此结构或修改它以最适合你的需求。 diff --git a/src/content/docs/zh-cn/5x/starter/hello-world.mdx b/src/content/docs/zh-cn/5x/starter/hello-world.mdx index 629d697721..eaecd41dac 100644 --- a/src/content/docs/zh-cn/5x/starter/hello-world.mdx +++ b/src/content/docs/zh-cn/5x/starter/hello-world.mdx @@ -1,16 +1,14 @@ --- -title: Hello world example -description: Get started with Express.js by building a simple 'Hello World' application, demonstrating the basic setup and server creation for beginners. +title: Hello World 示例 +description: 通过构建一个简单的"Hello World"应用来开始使用 Express.js,为初学者演示基本的设置和服务器创建。 --- import Alert from '@components/primitives/Alert/Alert.astro'; -Embedded below is essentially the simplest Express app you can create. It is a single file app -— _not_ what you'd get if you use the [Express generator](/starter/generator), which -creates the scaffolding for a full app with numerous JavaScript files, Jade templates, and -sub-directories for various purposes. +下面嵌入的是你能创建的最简单的 Express 应用。它是一个单文件应用 +— _不同于_ 使用 [Express 生成器](/starter/generator) 得到的结果,后者会创建一个完整应用的脚手架,包含众多 JavaScript 文件、Jade 模板以及用于各种目的的子目录。 @@ -28,27 +26,24 @@ app.listen(port, () => { }); ``` -This app starts a server and listens on port 3000 for connections. The app responds with "Hello World!" for requests -to the root URL (`/`) or _route_. For every other path, it will respond with a **404 Not Found**. +这个应用启动了一个服务器并监听 3000 端口的连接。对于根 URL(`/`)或_路由_的请求,应用会响应"Hello World!"。对于其他任何路径,它将响应 **404 Not Found**。 -## Running Locally +## 本地运行 -First create a directory named `myapp`, change to it and run `npm init`. Then, install `express` as a dependency, as per the [installation guide](/starter/installing). +首先创建一个名为 `myapp` 的目录,进入该目录并运行 `npm init`。然后,按照[安装指南](/starter/installing)安装 `express` 作为依赖。 -In the `myapp` directory, create a file named `app.js` and copy the code from the example above. +在 `myapp` 目录中,创建一个名为 `app.js` 的文件并复制上面的示例代码。 -The `req` (request) and `res` (response) are the exact same objects that Node provides, so you can -invoke `req.pipe()`, `req.on('data', callback)`, and anything else you would do without Express -involved. +`req`(请求)和 `res`(响应)与 Node 提供的对象完全相同,因此你可以调用 `req.pipe()`、`req.on('data', callback)` 以及任何其他在没有 Express 参与时你会做的操作。 -Run the app with the following command: +使用以下命令运行应用: ```bash $ node app.js ``` -Then, load `http://localhost:3000/` in a browser to see the output. +然后,在浏览器中加载 `http://localhost:3000/` 查看输出。 diff --git a/src/content/docs/zh-cn/5x/starter/installing.mdx b/src/content/docs/zh-cn/5x/starter/installing.mdx index f6dc8220d4..4a2f4fba8a 100644 --- a/src/content/docs/zh-cn/5x/starter/installing.mdx +++ b/src/content/docs/zh-cn/5x/starter/installing.mdx @@ -1,40 +1,40 @@ --- -title: Installing -description: Learn how to install Express.js in your Node.js environment, including setting up your project directory and managing dependencies with npm. +title: 安装 +description: 学习如何在 Node.js 环境中安装 Express.js,包括设置项目目录和使用 npm 管理依赖。 --- import Alert from '@components/primitives/Alert/Alert.astro'; -Before you begin, make sure you have [Node.js](https://nodejs.org/) 18 or higher installed. Then, create a directory for your application and navigate into it. +在开始之前,请确保你已安装 [Node.js](https://nodejs.org/) 18 或更高版本。然后,为你的应用创建一个目录并进入该目录。 ```bash mkdir myapp cd myapp ``` -Use the `npm init` command to create a `package.json` file for your application. -For more information on how `package.json` works, see [Specifics of npm's package.json handling](https://docs.npmjs.com/files/package.json). +使用 `npm init` 命令为你的应用创建一个 `package.json` 文件。 +有关 `package.json` 的更多信息,请参阅 [npm 的 package.json 处理说明](https://docs.npmjs.com/files/package.json)。 ```bash npm init ``` -This command prompts you for a number of things, such as the name and version of your application. -For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception: +该命令会提示你输入一些信息,例如应用的名称和版本。 +现在,你可以直接按回车键接受大多数选项的默认值,但以下选项除外: ``` entry point: (index.js) ``` -Enter `app.js`, or whatever you want the name of the main file to be. If you want it to be `index.js`, hit RETURN to accept the suggested default file name. +输入 `app.js`,或你想要的入口文件名称。如果希望是 `index.js`,直接按回车键接受建议的默认文件名即可。 -Now, install Express in the `myapp` directory and save it in the dependencies list. For example: +现在,在 `myapp` 目录中安装 Express 并将其保存到依赖列表中。例如: ```bash npm install express ``` -To install Express temporarily and not add it to the dependencies list: +如果只想临时安装 Express 而不将其添加到依赖列表中: ```bash npm install express --no-save diff --git a/src/content/docs/zh-cn/5x/starter/static-files.mdx b/src/content/docs/zh-cn/5x/starter/static-files.mdx index dbadee731a..7df13e6bc3 100644 --- a/src/content/docs/zh-cn/5x/starter/static-files.mdx +++ b/src/content/docs/zh-cn/5x/starter/static-files.mdx @@ -1,28 +1,28 @@ --- -title: Serving static files in Express -description: Understand how to serve static files like images, CSS, and JavaScript in Express.js applications using the built-in 'static' middleware. +title: 在 Express 中提供静态文件 +description: 了解如何使用内置的 'static' 中间件在 Express.js 应用中提供静态文件,如图片、CSS 和 JavaScript 文件。 --- import Alert from '@components/primitives/Alert/Alert.astro'; -To serve static files such as images, CSS files, and JavaScript files, use the `express.static` built-in middleware function in Express. +要提供图片、CSS 文件和 JavaScript 文件等静态文件,请使用 Express 中内置的 `express.static` 中间件函数。 -The function signature is: +该函数的签名为: ```js express.static(root, [options]); ``` -The `root` argument specifies the root directory from which to serve static assets. -For more information on the `options` argument, see [express.static](/api#express.static). +`root` 参数指定提供静态资产的根目录。 +有关 `options` 参数的更多信息,请参阅 [express.static](/api#express.static)。 -For example, use the following code to serve images, CSS files, and JavaScript files in a directory named `public`: +例如,使用以下代码来提供名为 `public` 目录中的图片、CSS 文件和 JavaScript 文件: ```js app.use(express.static('public')); ``` -Now, you can load the files that are in the `public` directory: +现在,你可以加载 `public` 目录中的文件: ```text http://localhost:3000/images/kitten.jpg @@ -34,35 +34,32 @@ http://localhost:3000/hello.html -Express looks up the files relative to the static directory, so the name of the static directory -is not part of the URL. +Express 会相对于静态目录查找文件,因此静态目录的名称不会包含在 URL 中。 -To use multiple static assets directories, call the `express.static` middleware function multiple times: +要使用多个静态资产目录,请多次调用 `express.static` 中间件函数: ```js app.use(express.static('public')); app.use(express.static('files')); ``` -Express looks up the files in the order in which you set the static directories with the `express.static` middleware function. +Express 会按照你使用 `express.static` 中间件函数设置静态目录的顺序来查找文件。 -For best results, use a reverse -proxy cache to improve performance of -serving static assets. +为了获得最佳效果,请使用反向代理缓存来提高提供静态资产的性能。 -To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the `express.static` function, [specify a mount path](/api#app.use) for the static directory, as shown below: +要为 `express.static` 函数提供的文件创建一个虚拟路径前缀(该路径在文件系统中实际不存在),请为静态目录[指定一个挂载路径](/api#app.use),如下所示: ```js app.use('/static', express.static('public')); ``` -Now, you can load the files that are in the `public` directory from the `/static` path prefix. +现在,你可以从 `/static` 路径前缀加载 `public` 目录中的文件。 ```text http://localhost:3000/static/images/kitten.jpg @@ -72,11 +69,11 @@ http://localhost:3000/static/images/bg.png http://localhost:3000/static/hello.html ``` -However, the path that you provide to the `express.static` function is relative to the directory from where you launch your `node` process. If you run the express app from another directory, it's safer to use the absolute path of the directory that you want to serve: +但是,你提供给 `express.static` 函数的路径是相对于启动 `node` 进程的目录的。如果你从其他目录运行 Express 应用,使用要提供服务的目录的绝对路径会更安全: ```js const path = require('path'); app.use('/static', express.static(path.join(__dirname, 'public'))); ``` -For more details about the `serve-static` function and its options, see [serve-static](/resources/middleware/serve-static). +有关 `serve-static` 函数及其选项的更多详细信息,请参阅 [serve-static](/resources/middleware/serve-static)。 diff --git a/src/i18n/ui/zh-cn.json b/src/i18n/ui/zh-cn.json index 85830318f2..360d4bde64 100644 --- a/src/i18n/ui/zh-cn.json +++ b/src/i18n/ui/zh-cn.json @@ -1,188 +1,188 @@ { "home": { - "welcome": "Welcome", - "workInProgress": "This is a work-in-progress homepage.", + "welcome": "欢迎", + "workInProgress": "这是一个正在开发中的主页。", "reviewDesignSystem": { - "prefix": "Review the", - "link": "Design System Foundations", - "suffix": "demo page to explore all tokens, primitives, and patterns." + "prefix": "查看", + "link": "设计系统基础", + "suffix": "演示页面以探索所有设计令牌、基础元素和模式。" } }, "search": { - "placeholder": "Start typing...", - "ariaLabel": "Start typing to search" + "placeholder": "开始输入...", + "ariaLabel": "开始输入以搜索" }, "theme": { - "toggle": "Toggle theme", - "switchToLight": "Switch to light mode", - "switchToDark": "Switch to dark mode" + "toggle": "切换主题", + "switchToLight": "切换到浅色模式", + "switchToDark": "切换到深色模式" }, "version": { - "selectLabel": "Select API version" + "selectLabel": "选择 API 版本" }, "language": { - "selectLabel": "Select language" + "selectLabel": "选择语言" }, "nav": { - "mainMenu": "Main menu", - "toggleMenu": "Toggle menu", - "home": "home", - "breadcrumb": "Breadcrumb", - "mainNavigation": "Main navigation", - "selectVersion": "Select documentation version" + "mainMenu": "主菜单", + "toggleMenu": "切换菜单", + "home": "首页", + "breadcrumb": "面包屑", + "mainNavigation": "主导航", + "selectVersion": "选择文档版本" }, "menu": { "main": { - "docs": "Docs", + "docs": "文档", "api": "API", - "resources": "Resources", - "blog": "Blog", - "support": "Support" + "resources": "资源", + "blog": "博客", + "support": "支持" }, "sections": { - "gettingStarted": "Getting started", - "guide": "Guide", - "migration": "Migration guides", - "advanced": "Advanced topics", + "gettingStarted": "快速入门", + "guide": "指南", + "migration": "迁移", + "advanced": "高级", "express": "express()", - "application": "Application", - "request": "Request", - "response": "Response", - "router": "Router", - "middleware": "Middleware" + "application": "应用", + "request": "请求", + "response": "响应", + "router": "路由", + "middleware": "中间件" }, "items": { - "installing": "Installing", - "helloWorld": "Hello world", - "expressGenerator": "Express generator", - "basicRouting": "Basic routing", - "staticFiles": "Serving static files", - "examples": "Examples", - "faq": "FAQ", - "routing": "Routing", - "writingMiddleware": "Writing middleware", - "usingMiddleware": "Using middleware", - "usingTemplateEngines": "Using template engines", - "errorHandling": "Error handling", - "debugging": "Debugging", - "behindProxies": "Behind proxies", - "databaseIntegration": "Database integration", - "overridingExpressApi": "Overriding the Express API", - "migratingTo4": "Moving to Express 4", - "migratingTo5": "Moving to Express 5", - "buildingTemplateEngines": "Building template engines", - "bestPracticePerformance": "Performance best practices", - "bestPracticeSecurity": "Security best practices", - "healthcheckGracefulShutdown": "Health checks & graceful shutdown", - "securityUpdates": "Security updates", - "overview": "Overview", - "properties": "Properties", - "methods": "Methods", - "events": "Events", - "types": "Built-in", - "community": "Community", - "glossary": "Glossary", - "contributing": "Contributing", - "utils": "Utilities", - "changelog": "Changelog", - "middleware": "Middleware" + "installing": "安装", + "helloWorld": "Hello World", + "expressGenerator": "Express 生成器", + "basicRouting": "基础路由", + "staticFiles": "提供静态文件", + "examples": "示例", + "faq": "常见问题", + "routing": "路由", + "writingMiddleware": "编写中间件", + "usingMiddleware": "使用中间件", + "usingTemplateEngines": "使用模板引擎", + "errorHandling": "错误处理", + "debugging": "调试", + "behindProxies": "代理服务器", + "databaseIntegration": "数据库集成", + "overridingExpressApi": "覆盖 Express API", + "migratingTo4": "迁移到 Express 4", + "migratingTo5": "迁移到 Express 5", + "buildingTemplateEngines": "构建模板引擎", + "bestPracticePerformance": "性能最佳实践", + "bestPracticeSecurity": "安全最佳实践", + "healthcheckGracefulShutdown": "健康检查与优雅关闭", + "securityUpdates": "安全更新", + "overview": "概述", + "properties": "属性", + "methods": "方法", + "events": "事件", + "types": "内置类型", + "community": "社区", + "glossary": "术语表", + "contributing": "贡献指南", + "utils": "工具函数", + "changelog": "更新日志", + "middleware": "中间件" }, "aria": { - "docs": "Documentation", - "api": "API Reference", - "resources": "Resources", - "blog": "Blog", - "support": "Support", - "installing": "Installing Express", - "helloWorld": "Hello world example", - "expressGenerator": "Express generator", - "routing": "Routing guide", - "writingMiddleware": "Writing middleware guide", - "usingMiddleware": "Using middleware guide", - "usingTemplateEngines": "Using template engines guide", - "errorHandling": "Error handling guide", - "debugging": "Debugging Express", - "behindProxies": "Express behind proxies", - "databaseIntegration": "Database integration guide", - "overridingExpressApi": "Overriding the Express API guide", - "application": "Application overview", - "request": "Request overview", - "response": "Response overview", - "router": "Router overview", - "middleware": "Middleware resources", - "community": "Community resources", - "glossary": "Glossary of terms", - "contributing": "Contributing guide", - "utils": "Utilities", - "changelog": "Express changelog" + "docs": "文档", + "api": "API 参考", + "resources": "资源", + "blog": "博客", + "support": "支持", + "installing": "安装 Express", + "helloWorld": "Hello World 示例", + "expressGenerator": "Express 生成器", + "routing": "路由指南", + "writingMiddleware": "编写中间件指南", + "usingMiddleware": "使用中间件指南", + "usingTemplateEngines": "使用模板引擎指南", + "errorHandling": "错误处理指南", + "debugging": "调试 Express", + "behindProxies": "代理后的 Express", + "databaseIntegration": "数据库集成指南", + "overridingExpressApi": "覆盖 Express API 指南", + "application": "应用概述", + "request": "请求概述", + "response": "响应概述", + "router": "路由概述", + "middleware": "中间件资源", + "community": "社区资源", + "glossary": "术语表", + "contributing": "贡献指南", + "utils": "工具函数", + "changelog": "Express 更新日志" } }, "doc": { - "previousPage": "Previous", - "nextPage": "Next", - "pageNavigation": "Page navigation" + "previousPage": "上一页", + "nextPage": "下一页", + "pageNavigation": "页面导航" }, "page": { - "editOnGitHub": "Edit on GitHub", - "translateThis": "Translate this page" + "editOnGitHub": "在 GitHub 上编辑", + "translateThis": "翻译此页面" }, "announcement": { - "readMore": "Read more", - "checkLatestBlog": "Check out our latest blog" + "readMore": "阅读更多", + "checkLatestBlog": "查看我们的最新博客" }, "post": { - "share": "Share", - "shareOnX": "Share on X", - "shareOnBluesky": "Share on Bluesky", - "shareOnLinkedIn": "Share on LinkedIn", - "shareOnSlack": "Share on Slack", - "shareOnTelegram": "Share on Telegram", - "shareOnWhatsApp": "Share on WhatsApp", - "rssFeed": "RSS Feed", - "copyLink": "Copy link", - "linkCopied": "Link copied!", - "toc": "On this page", - "more": "More from Express", - "writeBanner": "Interested in writing a post? \n Check out our guidelines to get started.", - "writeGuidelines": "Read the guidelines" + "share": "分享", + "shareOnX": "在 X 上分享", + "shareOnBluesky": "在 Bluesky 上分享", + "shareOnLinkedIn": "在 LinkedIn 上分享", + "shareOnSlack": "在 Slack 上分享", + "shareOnTelegram": "在 Telegram 上分享", + "shareOnWhatsApp": "在 WhatsApp 上分享", + "rssFeed": "RSS 订阅", + "copyLink": "复制链接", + "linkCopied": "链接已复制!", + "toc": "本页内容", + "more": "更多 Express 内容", + "writeBanner": "有兴趣撰写文章?\n 查看我们的指南以开始。", + "writeGuidelines": "阅读指南" }, "toc": { - "overview": "Overview" + "overview": "概述" }, "features": { - "title": "Clarity over complexity. For every developer.", + "title": "清晰胜于复杂。面向每一位开发者。", "performance": { - "title": "Performance", - "body": "Express provides a thin layer of fundamental web application features, without obscuring Node.js features that you know and love." + "title": "性能", + "body": "Express 提供了基本 Web 应用功能的精简层,不会掩盖你了解和喜爱的 Node.js 特性。" }, "api": { - "title": "APIs", - "body": "With a myriad of HTTP utility methods and middleware at your disposal, creating a robust API is quick and easy." + "title": "API", + "body": "借助丰富的 HTTP 工具方法和中间件,创建强大的 API 变得快速而简单。" }, "middleware": { - "title": "Middleware", - "body": "Express is a lightweight and flexible routing framework with minimal core features meant to be augmented through the use of Express middleware modules." + "title": "中间件", + "body": "Express 是一个轻量且灵活的路由框架,其核心功能极简,旨在通过 Express 中间件模块进行扩展。" }, "webapplication": { - "title": "Web Applications", - "body": "Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications." + "title": "Web 应用", + "body": "Express 是一个极简且灵活的 Node.js Web 应用框架,为 Web 和移动应用提供强大的功能集。" } }, "error404": { - "title": "404 - Page Not Found", - "description": "The page you are looking for could not be found.", - "heading": "Page Not Found", - "message": "The page you are looking for does not exist or has been moved.", - "goHome": "Go to Home" + "title": "404 - 页面未找到", + "description": "找不到您正在查找的页面。", + "heading": "页面未找到", + "message": "您正在查找的页面不存在或已被移动。", + "goHome": "返回首页" }, "common": { - "download": "Download" + "download": "下载" }, "hero": { - "tagline": "Fast, unopinionated, minimalist web framework for Node.js", - "getStarted": "Get Started", - "kawaiiLogoAlt": "Express.js kawaii logo", - "videoPause": "Pause background video", - "videoPlay": "Play background video" + "tagline": "快速、开放、极简的 Node.js Web 框架", + "getStarted": "开始使用", + "kawaiiLogoAlt": "Express.js 可爱版 Logo", + "videoPause": "暂停背景视频", + "videoPlay": "播放背景视频" } }