- 快速入门
- 指南
- 参考手册
- 高级主题
- 资源
- Express 词汇表
- Express 社区
- 模板引擎
- Express 中间件
- Express body-parser middleware
- Express compression middleware
- Express connect-rid middleware
- Express cookie-parser middleware
- Express cookie-session middleware
- Express cors middleware
- Express csurf middleware
- Express errorhandler middleware
- Express method-override middleware
- Express morgan middleware
- Express multer middleware
- Express response-time middleware
- Express serve-favicon middleware
- Express serve-index middleware
- Express serve-static middleware
- Express session middleware
- Express timeout middleware
- Express vhost middleware
- Express 实用模板
- 推荐框架
- 推荐书籍和博客
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:
Also, there are third-party extensions for Express, which simplify some of these patterns:
如何定义模型(model)?
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.
See LoopBack for an Express-based framework that is centered around models.
如何验证用户?
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 支持哪些模板引擎?
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.
For more information, see Using template engines with 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(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.
如何设置一个错误处理器?
错误处理器中间件的定义和其他中间件一样,唯一的区别是 4 个而不是 3 个参数,即 (err, req, res, next)
:
app.use(function (err, req, res, next) {
console.error(err.stack)
res.status(500).send('Something broke!')
})
请参考 错误处理 章节以了解更多信息。
如何渲染纯 HTML 文件?
不需要!无需通过 res.render()
渲染 HTML。 你可以通过 res.sendFile()
直接对外输出 HTML 文件。 如果你需要对外提供的资源文件很多,可以使用 express.static()
中间件。
Previous: Static Files
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论