forked from expressjs/expressjs.com
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfaq.html
40 lines (40 loc) · 7.18 KB
/
faq.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html><html><head><title>Express - 问答</title><link rel="stylesheet" href="/style.css"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="keywords" content="node.js express 中文文档, node express 中文, node express中文文档, express 翻译 api, expressjs 文档, exress框架 中文手册, node expressjs 中文文档, expressjs 中文文档, node express中文, Express框架的中文, express 中文 node, node.js网络应用, express node 文档, express.js 中文, express 中文文档"><script src="http://libs.baidu.com/jquery/1.7.2/jquery.js"></script><script src="/app.js"></script><script src="/retina.js"></script></head><body class="inner"><div class="bar"></div><section id="content"><header><section id="logo"><span class="express">express<em>3.0.4</em></span><span class="description">
web application framework for <a href="http://nodejs.org">node </a></span></section><nav class="clearfix"><a href="/" target="_self"> 首页</a><a href="/3x_zh-cn/api.html" target="_self"> API 参考</a><a href="/guide.html" target="_self"> 新手指南 </a><a href="/applications.html" target="_self"> 应用展示</a><a href="/community.html" target="_self"> 社区</a><a href="/faq.html" target="_self" class="active"> 问答</a><a href="/jade.html" target="_self"> Jade文档</a><a href="/4x_zh-cn/api.html" target="_self"> 4.x文档</a><a href="http://www.ksmimi.com/dashixiongwx/shop/1/article/508" target="_blank"> 工作机会</a><a href="http://123.jser.us" target="_blank"> 前端导航站 </a></nav></header><div id="faq"><section><h3 id="models">怎样定义模型?</h3><p>Express 根本没有涉及到数据库,这个任务留给了第三方的node模块,有了第三方的模块基本上可以与任何数据库交互
</p></section><section><h3 id="auth">怎样做用户认证?</h3><p>这是另一个Express不会做的事情, 你可以使用任何你想用的认证方案,<a href="https://github.com/visionmedia/express/tree/master/examples/auth">这里有一个简单的例子</a>。
</p></section><section><h3 id="templates">Express 支持哪个模板引擎?</h3><p>任何遵守这样回调的 <code>(path, locals, callback)</code> .
为了统一模板引擎接口和缓存,推荐查看<a href="https://github.com/visionmedia/consolidate.js">consolidate.js</a>
寻找帮助. 有些没有列出来的模板引擎没准也支持Express.
</p></section><section><h3 id="structure">我应该怎样组织我的程序结构?</h3><p>事实上这个没有一个标准答案,这与你的程序规模和团队强烈相关。
为了尽可能的灵活,Express没有规定程序的结构
</p><p>你可以把路由和其它的一些程序特定的逻辑代码以任意的目录结构任意数量的文件存放。
查看下面的例子找点灵感
</p><ul><li><a href="https://github.com/visionmedia/express/blob/master/examples/route-separation/index.js#L19">Route listings</a></li><li><a href="https://github.com/visionmedia/express/blob/master/examples/route-map/index.js#L47">Route map</a></li><li><a href="https://github.com/visionmedia/express/tree/master/examples/route-loading">Route bootstrapping</a></li><li><a href="https://github.com/visionmedia/express/tree/master/examples/mvc">MVC style controllers</a></li></ul><p>已经存在的简化这些模式的第三方Express扩展:
</p><ul><li><a href="https://github.com/visionmedia/express-resource">Resourceful routing</a></li><li><a href="https://github.com/visionmedia/express-namespace">Namespaced routing</a></li></ul></section><section><h3 id="multiple-statics">我应该怎样从多个目录提供静态文件服务?</h3><p>你可以会在你的程序中多次使用任意一个中间件。
使用下面的方式,当你请求"GET /javascripts/jquery.js" 时,会先检查 "./public/javascripts/jquery.js",
如果它不存在,随后的中间件会检查 "./files/javascripts/jquery.js".
</p><pre class="js"><code>app.use(express.static('public'));
app.use(express.static('files'));
</code></pre></section><setup><h3 id="static-prefix">怎样在提供静态文件服务的时候加一个前缀路径名?</h3><p>Connect's的中间件绑定技术允许你指定一个路径名前缀, 一个常用的例子是你可以前缀一个根本不是请求路径中一部分的字符。
假设你要请求 "GET /files/javascripts/jquery.js", 你可以把中间件挂在 "/files", 暴露出 "/javascripts/jquery.js"作为
<code>req.url</code> 来让中间件为这个文件提供服务:
</p><pre class="js"><code>app.use('/public', express.static('public'));
</code></pre></setup><setup><h3 id="migration">怎么迁移 Express 2.x 应用程序?</h3><p>Express 2x甚至能支持到node 1.0, 所以可能没有必要由于Express 3x的重构和API改变就迁移,如果你对2x感觉良好,那就停留在那个版本上。真的要迁移可以看<a href="https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x">这里</a> 或者查看一下3.x的<a href="https://github.com/visionmedia/express/wiki/New-features-in-3.x">改动列表</a>
</p></setup><setup><h3 id="404-handling">怎么处理404s?</h3><p>在Express里404s不被认为是出错的结果,所以错误处理中间件不会捕获404s,这是因为一个404只是由于有一些额外的工作没有做,换而言之,Express已经执行了所有的中间件 / 路由分发,然而没有发现有返回。你所要做的仅仅是在代码底部加一个中间件去处理没有返回的情况,并且手动返回一个404
</p><pre class="js"><code>app.use(function(req, res, next){
res.send(404, 'Sorry cant find that!');
});
</code></pre></setup><setup><h3 id="error-handling">Express里怎样处理异常?</h3><p>定义错误处理的中间件跟定义普通的中间件没有什么区别,仅仅是参数必须定义为4个,它们定义如下
<code>(err, req, res, next)</code>:
</p><pre class="js"><code>app.use(function(err, req, res, next){
console.error(err.stack);
res.send(500, 'Something broke!');
});
</code></pre><p>查看 <a href="/guide.html#error-handling">错误处理</a>, 获取更多信息
</p></setup><setup><h3 id="render-html">怎样输出纯HTML文件?</h3><p>不要这么做!根本没有必要直接使用<code>res.render()</code>输出HTML文件,
如果你有一个特定的文件应该使用 <code>res.sendfile()</code>,如果你要使用一个目录里大量的静态资源提供服务,请使用 <code>express.static()</code> 中间件.
</p></setup><setup><h3 id="size">Express的代码库有多大?</h3><p>Express 是一个非常小的框架,3.0.0正式发布版只有932行源码,Express强烈依赖的Connect只有267行源码,Connect可选的中间件和扩展总共1143行源码,并且只有到使用时才会加载</p></setup></div></section><script>(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'hm.baidu.com/h.js?47a437a0834ace3da0dac1156ed8369f';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script><a id="top" href="#"><img src="/images/arrow.png"></a><footer><div id="footer-content">© 2012 TJ Holowaychuk. 版权所有. <a href="http://jser.me">草依山</a>翻译</div></footer></body></html>