forked from expressjs/expressjs.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaq.html
74 lines (73 loc) · 7.02 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html><html><head><title>Express - faq</title><link rel="stylesheet" href="style.css"><link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700&subset=latin,latin-ext"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.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.0</em></span><span class="description">
web application framework for <a href="http://nodejs.org">node </a></span></section><nav class="clearfix"><a href="/" class=""> Home</a><a href="/api.html" class=""> API Reference</a><a href="/guide.html" class=""> Guide</a><a href="/applications.html" class=""> Applications</a><a href="/community.html" class=""> Community</a><a href="/faq.html" class="active"> FAQ</a></nav></header><div id="faq"><section><h3 id="models">How do I define models?</h3><p>Express has no notion of a database at all, this is
left up to third-party node modules - allowing you to
interface with nearly any database.
</p></section><section><h3 id="auth">How can I authenticate users?</h3><p>This is another opinionated area that Express does not
venture into, you may use any authentication scheme you wish.
For a simple username / password scheme view this <a href="https://github.com/visionmedia/express/tree/master/examples/auth">example</a>.
</p></section><section><h3 id="templates">Which template engines does Express support?</h3><p>Anything that can conform with the <code>(path, locals, callback)</code> signature.
To normalize template engine interfaces and caching it's recommended to
check the <a href="https://github.com/visionmedia/consolidate.js">consolidate.js</a>
project for support. Unlisted template engines may still support the Express
signature.
</p></section><section><h3 id="structure">How should I structure my application?</h3><p>There is no true answer to this question, it is highly dependant
on the scale of your application and the team involved. To be as
flexible as possible Express makes no assumptions in terms of structure.
</p><p>Routes and other application-specific logic may live in as many files
as you wish, in any directory structure you prefer. View the following
examples for inspiration:
</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>Available as well are third-party extensions to Express to simplify some of these patterns:
</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">How can I serve statics from several directories?</h3><p>You may typically use any middleware several times
within your application. With the following middleware setup and a request
to "GET /javascripts/jquery.js" would first check "./public/javascripts/jquery.js",
if it does not exist then the subsequent middleware will check "./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">How can I prefix a pathname for serving statics?</h3><p>Connect's generic "mounting" feature allows you to define
the pathname "prefix" to which the middleware will be invoked,
effectively behaving as if that prefix string was never
part of the path. Suppose if you wanted "GET /files/javascripts/jquery.js",
you could mount the middleware at "/files", exposing "/javascripts/jquery.js"
as the <code>req.url</code> allowing the middleware to serve the file:
</p><pre class="js"><code>app.use('/public', express.static('public'));
</code></pre></setup><setup><h3 id="migration">How do I migrate my Express 2.x application?</h3><p>Express 2x will likely be supported through to node 1.0 so there's
no immediate reason to update beyond the refactoring and API changes
that Express 3x introduces, so if you're happy with 2x feel free
to remain on that branch. For migration information visit the
<a href="https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x">migration</a>
wiki page, or view a <a href="https://github.com/visionmedia/express/wiki/New-features-in-3.x">list of changes</a> made in 3.x.
</p></setup><setup><h3 id="404-handling">How do you handle 404s?</h3><p>In Express 404s are not the result of an error, thus the
error-handler middleware will not capture 404s, this is
because a 404 is simply the absence of additional work to do,
in other words Express has executed all middleware / routes
and found that none of them responded. All you need to
do is add a middleware at the very bottom below all the others
to handle a 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">How do you setup an error handler in Express?</h3><p>Error-handling middleware are defined just like regular middleware,
however must be define with an arity of 4, that is the signature
<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>View <a href="/guide.html#error-handling">error-handling</a> for more information.
</p></setup><setup><h3 id="render-html">How do I render plain HTML?</h3><p>You don't! There's no need to "render" HTML with <code>res.render()</code>,
if you have a specific file you should use <code>res.sendfile()</code>, or
if you are serving many assets from a directory use the <code>express.static()</code>
middleware.
</p></setup><setup><h3 id="size">How big is the Express codebase?</h3><p>Express is a very small framework, the 3.0.0 release is only
932 SLOC, and the mandatory portion of Connect which Express
is built on is only 267 SLOC. The optional middleware bundled
with Connect add an additional 1143 SLOC, and are lazy loaded
upon use.</p></setup></div></section><script>var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-25235225-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
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. All rights reserved.</div></footer></body></html>