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. Express没有数据库的概念。This concept is left up to third-party Node modules, allowing you to interface with nearly any database.这个概念留给第三方节点模块,允许您与几乎任何数据库进行接口。

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. 身份验证是Express不涉足的另一个固执己见的领域。You may use any authentication scheme you wish. 您可以使用任何想要的身份验证方案。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 (path, locals, callback) signature. Express支持任何符合(path, locals, callback)签名的模板引擎。To normalize template engine interfaces and caching, see the consolidate.js project for support. 要规范化模板引擎接口和缓存,请参阅consolidate.js项目以获取支持。Unlisted template engines might still support the Express signature.未列出的模板引擎可能仍然支持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(function (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(function (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()中间件函数。

Previous: More examples