How do I define models?
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.
How can I authenticate users?
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 example.
Which template engines does Express support?
Anything that can conform with the (path, locals, callback) signature.
To normalize template engine interfaces and caching it's recommended to
check the consolidate.js
project for support. Unlisted template engines may still support the Express
signature.
How should I structure my application?
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.
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:
Available as well are third-party extensions to Express to simplify some of these patterns:
How can I serve statics from several directories?
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".
app.use(express.static('public'));
app.use(express.static('files'));
How can I prefix a pathname for serving statics?
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 req.url allowing the middleware to serve the file:
app.use('/public', express.static('public'));
How do I migrate my Express 2.x application?
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 migration wiki page, or view a list of changes made in 3.x.
How do you handle 404s?
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:
app.use(function(req, res, next){
res.send(404, 'Sorry cant find that!');
});
How do you setup an error handler in Express?
Error-handling middleware are defined just like regular middleware,
however must be define with an arity of 4, that is the signature
(err, req, res, next):
app.use(function(err, req, res, next){
console.error(err.stack);
res.send(500, 'Something broke!');
});
View error-handling for more information.
How do I render plain HTML?
You don't! There's no need to "render" HTML with res.render(),
if you have a specific file you should use res.sendfile(), or
if you are serving many assets from a directory use the express.static()
middleware.
How big is the Express codebase?
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.
