ExpressおよびStrongLoopからNode.jsのトレーニング

このドキュメントは、英語でのドキュメントへの時代遅れの相対的な可能性があります。最新の更新プログラムのために、英語でのドキュメントを参照してください。

This document might be outdated relative to the documentation in English. For the latest updates, please refer the documentation in English.

FAQ

How should I structure my application?

There is no definitive answer to this question. It depends 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:

Also, there are third-party extensions for Express, which simplify some of these patterns:

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.

See LoopBack for an Express-based framework centered around models.

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, see this example.

Which template engines does Express support?

Express supports any template engine that conforms with the (path, locals, callback) signature. To normalize template engine interfaces and caching, see the consolidate.js project for support. Unlisted template engines may still support the Express signature.

How can I serve static files from several directories?

You may typically use any middleware several times within your application. With the following middleware setup, and a request for GET /javascripts/jquery.js, the first check would be ./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 static files?

Connect’s generic “mounting” feature allows you to define the pathname “prefix” to which the middleware will be invoked. This effectively behaves as if that prefix string were never part of the path. Suppose 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 you handle 404s?

In Express, 404s are not the result of an error. Therefore, 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 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?

You define error-handling middleware the same way as other middleware, except with four arguments instead of three; specifically with the signature (err, req, res, next):

app.use(function(err, req, res, next){
  console.error(err.stack);
  res.send(500, 'Something broke!');
});

For more information, see Error handling.

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, use res.sendFile(). If you are serving many assets from a directory use the express.static() middleware.