常见问题

FAQ

应该如何构建我的应用?

How should I structure my application?

这个问题没有确定的答案。答案取决于你的应用的规模和所涉及的团队。为了尽可能灵活,Express 在结构方面不做任何假设。

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.

路由和其他特定于应用的逻辑可以存在于你喜欢的任何目录结构中的任意数量的文件中。查看以下示例以获得灵感:

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:

此外,Express 还有第三方扩展,可以简化其中一些模式:

Also, there are third-party extensions for Express, which simplify some of these patterns:

如何定义模型?

How do I define models?

Express 没有数据库的概念。这个概念留给第三方 Node 模块,允许你与几乎任何数据库进行交互。

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 的框架,请参见 LoopBack

See LoopBack for an Express-based framework that is centered around models.

如何验证用户身份?

How can I authenticate users?

身份验证是 Express 不会涉足的另一个有态度的区域。你可以使用任何你希望的身份验证方案。有关简单的用户名/密码方案,请参阅 这个例子

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.

Express 支持哪些模板引擎?

Which template engines does Express support?

Express 支持任何符合 (path, locals, callback) 签名的模板引擎。要规范模板引擎接口和缓存,请参阅 consolidate.js 项目以获得支持。未列出的模板引擎可能仍支持 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 project for support. Unlisted template engines might still support the Express signature.

有关详细信息,请参阅 使用 Express 模板引擎

For more information, see Using template engines with Express.

如何处理 404 响应?

How do I handle 404 responses?

在 Express 中,404 响应不是错误的结果,因此错误处理程序中间件不会捕获它们。此行为是因为 404 响应仅表示没有其他工作要做;也就是说,Express 把所有的中间件函数和路由都执行了一遍,发现都没有响应。你需要做的就是在堆栈的最底部(在所有其他函数下方)添加一个中间件函数来处理 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:

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

在运行时在 express.Router() 的实例上动态添加路由,这样路由就不会被中间件函数取代。

Add routes dynamically at runtime on an instance of express.Router() so the routes are not superseded by a middleware function.

如何设置错误处理程序?

How do I setup an error handler?

你可以用与其他中间件相同的方式定义错误处理中间件,除了使用四个参数而不是三个;特别是签名 (err, req, res, next)

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):

app.use((err, req, res, next) => {
  console.error(err.stack)
  res.status(500).send('Something broke!')
})

有关详细信息,请参阅 错误处理

For more information, see Error handling.

如何渲染纯 HTML?

How do I render plain HTML?

你不用!无需使用 res.render() 函数来 “render” HTML。如果你有特定文件,请使用 res.sendFile() 函数。如果你从一个目录提供许多资源,请使用 express.static() 中间件函数。

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.