mirror of
https://github.com/expressjs/expressjs.com.git
synced 2026-02-21 19:41:33 +00:00
118 lines
4.6 KiB
Plaintext
118 lines
4.6 KiB
Plaintext
|
|
section
|
|
h3(id='models') How do I define models?
|
|
|
|
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.
|
|
|
|
section
|
|
h3(id='auth') How can I authenticate users?
|
|
|
|
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>.
|
|
|
|
section
|
|
h3(id='templates') Which template engines does Express support?
|
|
|
|
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.
|
|
|
|
section
|
|
h3(id='structure') How should I structure my application?
|
|
|
|
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.
|
|
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:
|
|
|
|
ul
|
|
li: a(href='https://github.com/visionmedia/express/blob/master/examples/route-separation/app.js#L20') Route listings
|
|
li: a(href='https://github.com/visionmedia/express/blob/master/examples/route-map/index.js#L47') Route map
|
|
li: a(href='https://github.com/visionmedia/express/tree/master/examples/route-loading') Route bootstrapping
|
|
li: a(href='https://github.com/visionmedia/express/tree/master/examples/mvc') MVC style controllers
|
|
|
|
p.
|
|
Available as well are third-party extensions to Express to simplify some of these patterns:
|
|
|
|
ul
|
|
li: a(href='https://github.com/visionmedia/express-resource') Resourceful routing
|
|
li: a(href='https://github.com/visionmedia/express-namespace') Namespaced routing
|
|
|
|
section
|
|
h3(id='multiple-statics') How can I serve statics from several directories?
|
|
|
|
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".
|
|
|
|
+js.
|
|
app.use(express.static('public'));
|
|
app.use(express.static('files'));
|
|
|
|
setup
|
|
h3(id='static-prefix') How can I prefix a pathname for serving statics?
|
|
|
|
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:
|
|
|
|
+js.
|
|
app.use('/public', express.static('public'));
|
|
|
|
setup
|
|
h3(id='migration') How do I migrate my Express 2.x application?
|
|
|
|
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.
|
|
|
|
setup
|
|
h3(id='error-handling') How do you setup an error handler in Express?
|
|
|
|
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>:
|
|
|
|
+js.
|
|
app.use(function(err, req, res, next){
|
|
console.error(err.stack);
|
|
res.send(500, 'Something broke!');
|
|
});
|
|
|
|
p.
|
|
View <a href="http://localhost:3000/guide.html#error-handling">error-handling</a> information.
|
|
|
|
setup
|
|
h3(id='size') How big is the Express codebase?
|
|
|
|
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.
|