mirror of
https://github.com/expressjs/expressjs.com.git
synced 2026-02-21 19:41:33 +00:00
51 lines
5.7 KiB
HTML
51 lines
5.7 KiB
HTML
<!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></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><div id="x"></div><div id="y"></div></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/app.js#L20">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="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="http://localhost:3000/guide.html#error-handling">error-handling</a> information.
|
|
</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><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> |