FAQ常见问题解答
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:路由和其他特定于应用程序的逻辑可以存在于任意数量的文件中,也可以存在于您喜欢的任何目录结构中。查看以下示例以获取灵感:
Also, there are third-party extensions for Express, which simplify some of these patterns:此外,Express还有第三方扩展,可以简化其中一些模式:
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 for an Express-based framework that is centered around models.有关以模型为中心的基于Express的框架,请参阅LoopBack。
How can I authenticate users?我如何对用户进行身份验证?
Authentication is another opinionated area that Express does not
venture into. You may use any authentication scheme you wish.身份验证是Express不涉足的另一个固执己见的领域。您可以使用任何您想要的身份验证方案。
For a simple username / password scheme, see this example.有关简单的用户名/密码方案,请参阅此示例。
Which template engines does Express support?Express支持哪些模板引擎?
Express supports any template engine that conforms with the Express支持任何符合(path, locals, callback)
signature.(path, locals, callback)
签名的模板引擎。
To normalize template engine interfaces and caching, see the
consolidate.js
project for support. Unlisted template engines might still support the Express signature.要规范化模板引擎接口和缓存,请参阅consolidate.js工程以获取支持。未列出的模板引擎可能仍支持Express签名。
For more information, see Using template engines with Express.有关更多信息,请参阅在Express中使用模板引擎。
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. 在Express中,404响应不是错误的结果,因此错误处理程序中间件不会捕获它们。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. 这种行为是因为404响应只是表示没有额外的工作要做;换句话说,Express已经执行了所有中间件功能和路由,但发现它们都没有响应。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:您需要做的就是在堆栈的最底部(在所有其他函数下方)添加一个中间件函数来处理404响应:
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)
:
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send('Something broke!')
})
For more information, see 有关更多信息,请参阅错误处理。Error handling错误处理.
How do I render plain HTML?如何呈现纯HTML?
You don’t! There’s no need to “render” HTML with the 你没有!不需要使用res.render()
function.res.render()
函数“渲染”HTML。
If you have a specific file, use the 如果您有特定的文件,请使用res.sendFile()
function.res.sendFile()
函数。
If you are serving many assets from a directory, use the 如果您正在从一个目录中提供许多资产,请使用express.static()
middleware function.express.static()
中间件函数。
What version of Node.js does Express require?Express需要什么版本的Node.js?
- Express 4.x
requires Node.js 0.10 or higher.需要Node.js 0.10或更高版本。 - Express 5.x
requires Node.js 18 or higher.需要Node.js 18或更高版本。