🌐 Nodejs.cn

常见问题

我应该如何构建我的应用?

🌐 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.

参见 [LoopBack](https://loopback.io/),这是一个以模型为核心的基于 Express 的框架。

🌐 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() 函数“渲染”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.

Express 需要哪个版本的 Node.js?

🌐 What version of Node.js does Express require?