FAQ
アプリケーションを構成するにはどうすればいいですか?
There is no definitive answer to this question. この質問には決定的な答えはありません。 The answer depends on the scale of your application and the team that is involved. 可能な限り 柔軟性を持たせるために、Expressは構造的な仮定をしません。
ルートやその他のアプリケーション固有のロジックは、任意のディレクトリ構造において、 好きな数のファイルに保存することができます。 View the following examples for inspiration: View the following examples for inspiration:
また、Express 用のサードパーティの拡張機能もあり、これらのパターンのいくつかを簡素化します。
モデルを定義するにはどうすればいいですか?
Express にはデータベースという概念はありません。 このコンセプトは サードパーティ製の Node モジュールに残されており、ほぼすべてのデータベースと インターフェイスを使用できます。 This concept is left up to third-party Node modules, allowing you to interface with nearly any database.
モデルを中心とした Express ベースのフレームワークについては LoopBack を参照してください。
どのようにユーザーを認証できますか?
Authentication is another opinionated area that Express does not venture into. You may use any authentication scheme you wish. 任意の認証スキームを使用できます。 単純なユーザー名/パスワードスキームについては、この例を参照してください。
Expressはどのテンプレートエンジンをサポートしていますか?
Express は、`(path, local, callback) 署名に適合するテンプレートエンジンをサポートしています。 テンプレートエンジンインターフェイスとキャッシュを正規化するには、 consolidate.js プロジェクトをサポートしてください。 リストされていないテンプレートエンジンは引き続きExpress 署名をサポートする可能性があります。 To normalize template engine interfaces and caching, see the consolidate.js project for support. Unlisted template engines might still support the Express signature.
詳しくは、Expressでテンプレートエンジンを使用するを参照してください。
404応答はどのように処理すればいいですか?
In Express, 404 responses are not the result of an error, so the error-handler middleware will not capture them. Expressでは404応答はエラーの結果ではないため、 エラーハンドラミドルウェアはそれらを捕捉しません。 This behavior is because a 404 response simply indicates the absence of additional work to do; in other words, Express has executed all middleware functions and routes, and found that none of them responded. All you need to do is add a middleware function at the very bottom of the stack (below all other functions) to handle a 404 response:
app.use((req, res, next) => { res.status(404).send("Sorry can't find that!");});import { type Request, type Response, type NextFunction } from 'express';
app.use((req: Request, res: Response, next: NextFunction) => { res.status(404).send("Sorry can't find that!");});express.Router()
のインスタンスで実行時にルートを動的に追加することで、ルートはミドルウェア関数に取って代わられません。
エラーハンドラの設定方法は?
他のミドルウェアと同じ方法でエラー処理ミドルウェア
を定義します。ただし、3つではなく4つの引数を使用します。 具体的にはシグネチャ(err, req, res, next)を指定します。
app.use((err, req, res, next) => { console.error(err.stack); res.status(500).send('Something broke!');});import { type Request, type Response, type NextFunction } from 'express';
app.use((err: Error, req: Request, res: Response, next: NextFunction) => { console.error(err.stack); res.status(500).send('Something broke!');});詳細については、Error handlingを参照してください。
プレーンHTMLをレンダリングするにはどうすればいいですか?
You don’t! 違います! res.render()関数でHTMLを「レンダリング」する必要はありません。
特定のファイルがある場合は、 res.sendFile() 関数を使用します。
ディレクトリから多くのアセットを提供している場合は、express.static()
ミドルウェア関数を使用してください。
If you have a specific file, use the res.sendFile() function.
If you are serving many assets from a directory, use the express.static()
middleware function.
Express にはどのバージョンの Node.js が必要ですか?
- Express 4.x では、Node.js 0.10 以上が必要です。
- Express 5.x](/api) ではNode.js 18 以上が必要です。