mirror of
https://github.com/expressjs/expressjs.com.git
synced 2026-02-21 19:41:33 +00:00
remove prism (#1746)
This commit is contained in:
committed by
GitHub
parent
fabc75a85a
commit
68b4840949
@@ -298,13 +298,13 @@ StrongLoop Process Manager を Systemd サービスとして簡単にインス
|
||||
|
||||
StrongLoop PM を Systemd サービスとしてインストールするには、次のようにします。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ sudo sl-pm-install --systemd
|
||||
```
|
||||
|
||||
次に、サービスを開始します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ sudo /usr/bin/systemctl start strong-pm
|
||||
```
|
||||
|
||||
@@ -366,13 +366,13 @@ StrongLoop Process Manager を Upstart サービスとして簡単にインス
|
||||
|
||||
StrongLoop PM を Upstart 1.4 サービスとしてインストールするには、次のようにします。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ sudo sl-pm-install
|
||||
```
|
||||
|
||||
次に、サービスを実行します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ sudo /sbin/initctl start strong-pm
|
||||
```
|
||||
|
||||
@@ -400,7 +400,7 @@ StrongLoop Process Manager (PM) は、アプリケーションを実行する際
|
||||
|
||||
例えば、アプリケーションを prod.foo.com にデプロイして、StrongLoop PM がポート 8701 (デフォルト) で listen している場合は、slc を使用してクラスター・サイズを 8 に設定します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ slc ctl -C http://prod.foo.com:8701 set-size my-app 8
|
||||
```
|
||||
|
||||
@@ -414,7 +414,7 @@ When running an application with PM2, you can enable **cluster mode** to run it
|
||||
|
||||
To enable cluster mode, start your application like so:
|
||||
|
||||
```console
|
||||
```bash
|
||||
# Start 4 worker processes
|
||||
$ pm2 start app.js -i 4
|
||||
# Auto-detect number of available CPUs and start that many worker processes
|
||||
@@ -425,7 +425,7 @@ This can also be configured within a PM2 process file (`ecosystem.config.js` or
|
||||
|
||||
Once running, a given application with the name `app` can be scaled like so:
|
||||
|
||||
```console
|
||||
```bash
|
||||
# Add 3 more workers
|
||||
$ pm2 scale app +3
|
||||
# Scale to a specific number of workers
|
||||
|
||||
@@ -58,7 +58,7 @@ Helmet は、実際には、セキュリティー関連の HTTP ヘッダーを
|
||||
|
||||
その他のモジュールと同様に Helmet をインストールします。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install --save helmet
|
||||
```
|
||||
|
||||
@@ -152,7 +152,7 @@ npm を使用したアプリケーションの依存関係の管理は、強力
|
||||
|
||||
npm@6以降、npmはすべてのインストール要求を自動的に確認します。また、'npm audit'を使用して依存関係ツリーを分析することもできます。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm audit
|
||||
```
|
||||
|
||||
@@ -160,14 +160,14 @@ $ npm audit
|
||||
|
||||
Snykは、[Snykのオープンソース脆弱性データベース](https://snyk.io/vuln/)に対して、依存関係の既知の脆弱性に対するアプリケーションをチェックする[コマンドラインツール](https://www.npmjs.com/package/snyk)と[Github integration](https://snyk.io/docs/github)を提供しています。 次のようにCLIをインストールします。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install -g snyk
|
||||
$ cd your-app
|
||||
```
|
||||
|
||||
このコマンドを使用して、アプリケーションの脆弱性をテストします。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ snyk test
|
||||
```
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ app.set('view engine', 'ntl') // register the template engine
|
||||
|
||||
これで、アプリケーションは `.ntl` ファイルをレンダリングできるようになります。以下のコンテンツで `index.ntl` というファイルを `views` ディレクトリーに作成します。
|
||||
|
||||
```text
|
||||
```pug
|
||||
#title#
|
||||
#message#
|
||||
```
|
||||
|
||||
@@ -37,7 +37,7 @@ description: Discover how to integrate various databases with Express.js applica
|
||||
|
||||
**インストール**
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install cassandra-driver
|
||||
```
|
||||
|
||||
@@ -59,7 +59,7 @@ client.execute('select key from system.local', (err, result) => {
|
||||
|
||||
### インストール
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install couchbase
|
||||
```
|
||||
|
||||
@@ -96,7 +96,7 @@ bucket.query(query, [13], (err, result) => {
|
||||
|
||||
**インストール**
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install nano
|
||||
```
|
||||
|
||||
@@ -132,7 +132,7 @@ books.list((err, body) => {
|
||||
|
||||
### インストール
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install level levelup leveldown
|
||||
```
|
||||
|
||||
@@ -159,7 +159,7 @@ db.put('name', 'LevelUP', (err) => {
|
||||
|
||||
### インストール
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install mysql
|
||||
```
|
||||
|
||||
@@ -191,7 +191,7 @@ connection.end()
|
||||
|
||||
**インストール**
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install mongodb
|
||||
```
|
||||
|
||||
@@ -237,7 +237,7 @@ MongoDB 用のオブジェクト・モデル・ドライバーが必要な場合
|
||||
|
||||
### インストール
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install apoc
|
||||
```
|
||||
|
||||
@@ -264,7 +264,7 @@ apoc.query('match (n) return n').exec().then(
|
||||
|
||||
NOTE: [See installation prerequisites](https://github.com/oracle/node-oracledb#-installation).
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install oracledb
|
||||
```
|
||||
|
||||
@@ -308,7 +308,7 @@ getEmployee(101)
|
||||
|
||||
### インストール
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install pg-promise
|
||||
```
|
||||
|
||||
@@ -333,7 +333,7 @@ db.one('SELECT $1 AS value', 123)
|
||||
|
||||
### インストール
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install redis
|
||||
```
|
||||
|
||||
@@ -368,7 +368,7 @@ client.hkeys('hash key', (err, replies) => {
|
||||
|
||||
### インストール
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install tedious
|
||||
```
|
||||
|
||||
@@ -424,7 +424,7 @@ function executeStatement () {
|
||||
|
||||
### インストール
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install sqlite3
|
||||
```
|
||||
|
||||
@@ -458,7 +458,7 @@ db.close()
|
||||
|
||||
### インストール
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install elasticsearch
|
||||
```
|
||||
|
||||
|
||||
@@ -11,19 +11,19 @@ description: Learn how to enable and use debugging logs in Express.js applicatio
|
||||
|
||||
Express で使用されているすべての内部ログを表示するには、アプリケーションの起動時に `DEBUG` 環境変数を `express:*` に設定します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ DEBUG=express:* node index.js
|
||||
```
|
||||
|
||||
Windows では、対応するコマンドを使用します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
> set DEBUG=express:* & node index.js
|
||||
```
|
||||
|
||||
[express ジェネレーター](/{{ page.lang }}/starter/generator.html) で生成されるデフォルトのアプリケーションでこのコマンドを実行すると、以下の出力が表示されます。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ DEBUG=express:* node ./bin/www
|
||||
express:router:route new / +0ms
|
||||
express:router:layer new / +1ms
|
||||
@@ -69,7 +69,7 @@ $ DEBUG=express:* node ./bin/www
|
||||
|
||||
その後、アプリケーションに対して要求が出されると、Express コードで指定された次のログが表示されます。
|
||||
|
||||
```console
|
||||
```bash
|
||||
express:router dispatching GET / +4h
|
||||
express:router query : / +2ms
|
||||
express:router expressInit : / +0ms
|
||||
@@ -95,12 +95,12 @@ $ DEBUG=express:* node ./bin/www
|
||||
|
||||
例えば、`$ express sample-app` を使用してアプリケーションを生成する場合、次のコマンドを使用してデバッグ・ステートメントを有効にすることができます。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ DEBUG=sample-app:* node ./bin/www
|
||||
```
|
||||
|
||||
名前のコンマ区切りリストを割り当てることで、複数のデバッグ名前空間を指定できます。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ DEBUG=http,mail,express:* node index.js
|
||||
```
|
||||
|
||||
@@ -357,7 +357,7 @@ http.createServer(app).listen(app.get('port'), () => {
|
||||
|
||||
移行プロセスを開始するには、次のコマンドを使用して、Express 4 アプリケーションに必要なミドルウェアをインストールし、Express と Pug をそれぞれの最新バージョンに更新します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install serve-favicon morgan method-override express-session body-parser multer errorhandler express@latest pug@latest --save
|
||||
```
|
||||
|
||||
@@ -463,7 +463,7 @@ app.listen(app.get('port'), () => {
|
||||
|
||||
これで移行プロセスは完了して、アプリケーションは Express 4 アプリケーションになりました。確認するには、次のコマンドを使用してアプリケーションを開始します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ node .
|
||||
```
|
||||
|
||||
@@ -477,7 +477,7 @@ Express アプリケーションを生成するためのコマンド・ライン
|
||||
|
||||
システムに Express 3 アプリケーション・ジェネレーターがインストールされている場合は、次のようにしてアンインストールする必要があります。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm uninstall -g express
|
||||
```
|
||||
|
||||
@@ -485,7 +485,7 @@ $ npm uninstall -g express
|
||||
|
||||
次に、新しいジェネレーターをインストールします。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install -g express-generator
|
||||
```
|
||||
|
||||
@@ -506,7 +506,7 @@ $ npm install -g express-generator
|
||||
|
||||
次のコマンドを実行して、Express 4 アプリケーションを作成します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ express app4
|
||||
```
|
||||
|
||||
@@ -516,7 +516,7 @@ $ express app4
|
||||
|
||||
依存関係をインストールした後、次のコマンドを使用してアプリケーションを開始します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm start
|
||||
```
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ Express 5 は Express 4 とあまり変わりません。API の変更は、3.0
|
||||
|
||||
最新のアルファをインストールして Express 5 をプレビューするには、アプリケーションのルート・ディレクトリーで次のコマンドを入力します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install "express@^{{ site.data.express.next_version }}" --save
|
||||
```
|
||||
|
||||
|
||||
@@ -230,7 +230,7 @@ Express アプリケーションに機能を追加するには、サード・パ
|
||||
|
||||
次の例は、Cookie 解析ミドルウェア関数 `cookie-parser` のインストールおよびロードを示しています。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install cookie-parser
|
||||
```
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ Expressで使用できるテンプレートエンジンのリストについて
|
||||
|
||||
次に、対応するテンプレートエンジンnpmパッケージをインストールします。たとえばPugをインストールするには:
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install pug --save
|
||||
```
|
||||
|
||||
|
||||
@@ -15,11 +15,9 @@ description: Learn the fundamentals of routing in Express.js applications, inclu
|
||||
各ルートには、1 つ以上のハンドラー関数があり、それらはルートが一致したときに実行されます。
|
||||
|
||||
ルート定義では、次の構造を使用します。
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
```js
|
||||
app.METHOD(PATH, HANDLER)
|
||||
</code>
|
||||
</pre>
|
||||
```
|
||||
|
||||
各部分の意味は次のとおりです。
|
||||
|
||||
@@ -36,42 +34,34 @@ app.METHOD(PATH, HANDLER)
|
||||
|
||||
ホーム・ページで `Hello World!` と応答します。
|
||||
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
app.get('/', function (req, res) {
|
||||
res.send('Hello World!');
|
||||
});
|
||||
</code>
|
||||
</pre>
|
||||
```js
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Hello World!')
|
||||
})
|
||||
```
|
||||
|
||||
アプリケーションのホーム・ページであるルートのルート (`/`) で POST 要求に応答します。
|
||||
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
app.post('/', function (req, res) {
|
||||
res.send('Got a POST request');
|
||||
});
|
||||
</code>
|
||||
</pre>
|
||||
```js
|
||||
app.post('/', (req, res) => {
|
||||
res.send('Got a POST request')
|
||||
})
|
||||
```
|
||||
|
||||
`/user` ルートに対する PUT 要求に応答します。
|
||||
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
app.put('/user', function (req, res) {
|
||||
res.send('Got a PUT request at /user');
|
||||
});
|
||||
</code>
|
||||
</pre>
|
||||
```js
|
||||
app.put('/user', (req, res) => {
|
||||
res.send('Got a PUT request at /user')
|
||||
})
|
||||
```
|
||||
|
||||
`/user` ルートに対する DELETE 要求に応答します。
|
||||
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
app.delete('/user', function (req, res) {
|
||||
res.send('Got a DELETE request at /user');
|
||||
});
|
||||
</code>
|
||||
</pre>
|
||||
```js
|
||||
app.delete('/user', (req, res) => {
|
||||
res.send('Got a DELETE request at /user')
|
||||
})
|
||||
```
|
||||
|
||||
ルーティングについて詳しくは、[ルーティング・ガイド](/{{ page.lang }}/guide/routing.html)を参照してください。
|
||||
|
||||
@@ -45,26 +45,22 @@ Express は、`(パス、ロケール、コールバック)` シグニチャー
|
||||
|
||||
Express では、404 応答はエラーの結果ではありません。そのため、エラー・ハンドラー・ミドルウェアはそれらをキャプチャーしません。このように動作するのは、404 応答は単に追加の処理が存在しないことを示しているためです。つまり、Express は、すべてのミドルウェア関数とルートを実行して、そのいずれも応答しなかったことを検出したということです。404 応答に対応するには、スタックの最下部 (他のすべての関数の下) にミドルウェア関数を追加するだけですみます。
|
||||
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
app.use(function(req, res, next) {
|
||||
res.status(404).send('Sorry cant find that!');
|
||||
});
|
||||
</code>
|
||||
</pre>
|
||||
```js
|
||||
app.use((req, res, next) => {
|
||||
res.status(404).send('Sorry cant find that!')
|
||||
})
|
||||
```
|
||||
|
||||
## どのようにしてエラー・ハンドラーをセットアップするのですか?
|
||||
|
||||
エラー処理ミドルウェアの定義方法は、他のミドルウェアと同じですが、引数の数が 3 つではなく 4 つである点が異なります。具体的には、シグニチャー `(err、req、res、next)` です。
|
||||
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
app.use(function(err, req, res, next) {
|
||||
console.error(err.stack);
|
||||
res.status(500).send('Something broke!');
|
||||
});
|
||||
</code>
|
||||
</pre>
|
||||
```js
|
||||
app.use((err, req, res, next) => {
|
||||
console.error(err.stack)
|
||||
res.status(500).send('Something broke!')
|
||||
})
|
||||
```
|
||||
|
||||
詳細については、[エラー処理](/{{ page.lang }}/guide/error-handling.html)を参照してください。
|
||||
|
||||
|
||||
@@ -13,13 +13,13 @@ description: Learn how to use the Express application generator tool to quickly
|
||||
|
||||
次のコマンドを使用して、`express` をインストールします。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install express-generator -g
|
||||
```
|
||||
|
||||
`-h` オプションを指定してコマンド・オプションを表示します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ express -h
|
||||
|
||||
Usage: express [options][dir]
|
||||
@@ -41,7 +41,7 @@ $ express -h
|
||||
|
||||
例えば、以下のコマンドでは、現行作業ディレクトリーに _myapp_ という Express アプリケーションを作成します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ express --view=pug myapp
|
||||
|
||||
create : myapp
|
||||
@@ -65,20 +65,20 @@ $ express --view=pug myapp
|
||||
|
||||
次に、依存関係をインストールします。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ cd myapp
|
||||
$ npm install
|
||||
```
|
||||
|
||||
MacOS または Linux では、次のコマンドによってアプリケーションを実行します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ DEBUG=myapp:* npm start
|
||||
```
|
||||
|
||||
Windows では、次のコマンドを使用します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
> set DEBUG=myapp:* & npm start
|
||||
```
|
||||
|
||||
@@ -86,7 +86,7 @@ Windows では、次のコマンドを使用します。
|
||||
|
||||
生成されたアプリケーションには、以下のディレクトリー構造があります。
|
||||
|
||||
```console
|
||||
```bash
|
||||
.
|
||||
├── app.js
|
||||
├── bin
|
||||
|
||||
@@ -17,8 +17,7 @@ description: Get started with Express.js by building a simple 'Hello World' appl
|
||||
|
||||
`myapp` ディレクトリーで、`app.js` というファイルを作成して、以下のコードを追加します。
|
||||
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
```js
|
||||
const express = require('express')
|
||||
const app = express()
|
||||
const port = 3000
|
||||
@@ -30,8 +29,7 @@ app.get('/', (req, res) => {
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}`)
|
||||
})
|
||||
</code>
|
||||
</pre>
|
||||
```
|
||||
|
||||
アプリケーションは、サーバーを始動して、ポート 3000 で接続を listen します。アプリケーションは、ルート URL (`/`) または*ルート* に対する要求に「Hello World!」と応答します。その他すべてのパスについては、「**404 Not Found**」と応答します。
|
||||
|
||||
@@ -41,7 +39,7 @@ app.listen(port, () => {
|
||||
|
||||
次のコマンドを使用してアプリケーションを実行します。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ node app.js
|
||||
```
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ description: Learn how to install Express.js in your Node.js environment, includ
|
||||
|
||||
既に [Node.js](https://nodejs.org/) をインストールしてあることを想定して、ここではアプリケーションを作成するディレクトリーを作り、それを作業ディレクトリーにします。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ mkdir myapp
|
||||
$ cd myapp
|
||||
```
|
||||
@@ -19,14 +19,14 @@ $ cd myapp
|
||||
`npm init` コマンドを使用して、アプリケーション用の `package.json` ファイルを作成します。
|
||||
`package.json` の機能について詳しくは、[Specifics of npm's package.json handling](https://docs.npmjs.com/files/package.json) を参照してください。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm init
|
||||
```
|
||||
|
||||
次のコマンドは、アプリケーションの名前やバージョンなど、いくつかのことを要求します。
|
||||
ここでは、以下の例外を除いて、RETURN キーを押して単にそれらのデフォルトのほとんどを受け入れることができます。
|
||||
|
||||
```console
|
||||
```bash
|
||||
entry point: (index.js)
|
||||
```
|
||||
|
||||
@@ -34,13 +34,13 @@ entry point: (index.js)
|
||||
|
||||
Expressを `myapp` ディレクトリにインストールし、それを依存関係リストに保存してください。例えば:
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install express
|
||||
```
|
||||
|
||||
Express を一時的にインストールし、それを依存関係リストに追加しないようにするには、次のようにします。
|
||||
|
||||
```console
|
||||
```bash
|
||||
$ npm install express --no-save
|
||||
```
|
||||
|
||||
|
||||
@@ -13,23 +13,19 @@ description: Understand how to serve static files like images, CSS, and JavaScri
|
||||
|
||||
静的アセットファイルを格納しているディレクトリーの名前を `express.static` ミドルウェア関数に渡して、ファイルの直接提供を開始します。例えば、`public` というディレクトリー内のイメージ、CSS ファイル、JavaScript ファイルを提供するには、次のコードを使用します。
|
||||
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
app.use(express.static('public'));
|
||||
</code>
|
||||
</pre>
|
||||
```js
|
||||
app.use(express.static('public'))
|
||||
```
|
||||
|
||||
これで、`public` ディレクトリーに入っているファイルをロードできます。
|
||||
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
```text
|
||||
http://localhost:3000/images/kitten.jpg
|
||||
http://localhost:3000/css/style.css
|
||||
http://localhost:3000/js/app.js
|
||||
http://localhost:3000/images/bg.png
|
||||
http://localhost:3000/hello.html
|
||||
</code>
|
||||
</pre>
|
||||
```
|
||||
|
||||
<div class="doc-box doc-info">
|
||||
Express は、静的ディレクトリーから相対的なファイルを検索するため、静的ディレクトリーの名前は URL の一部ではありません。
|
||||
@@ -37,39 +33,32 @@ Express は、静的ディレクトリーから相対的なファイルを検索
|
||||
|
||||
複数の静的アセットディレクトリーを使用するには、`express.static` ミドルウェア関数を複数回呼び出します。
|
||||
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
app.use(express.static('public'));
|
||||
app.use(express.static('files'));
|
||||
</code>
|
||||
</pre>
|
||||
```js
|
||||
app.use(express.static('public'))
|
||||
app.use(express.static('files'))
|
||||
```
|
||||
|
||||
Express は、`express.static` ミドルウェア関数に静的ディレクトリーが設定された順序でファイルを検索します。
|
||||
|
||||
`express.static` 関数によって提供されるファイルの仮想パスのプレフィックス (パスは実際にはファイル・システムに存在しません) を作成するには、次に示すように、静的ディレクトリーの[マウント・パスを指定](/{{ page.lang }}/4x/api.html#app.use)します。
|
||||
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
app.use('/static', express.static('public'));
|
||||
</code>
|
||||
</pre>
|
||||
```js
|
||||
app.use('/static', express.static('public'))
|
||||
```
|
||||
|
||||
これで、`public` ディレクトリー内のファイルを `/static` パス・プレフィックスからロードできます。
|
||||
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
```text
|
||||
http://localhost:3000/static/images/kitten.jpg
|
||||
http://localhost:3000/static/css/style.css
|
||||
http://localhost:3000/static/js/app.js
|
||||
http://localhost:3000/static/images/bg.png
|
||||
http://localhost:3000/static/hello.html
|
||||
</code>
|
||||
</pre>
|
||||
```
|
||||
|
||||
ただし、`express.static` 関数に指定するパスは、`node` プロセスを起動するディレクトリーに対して相対的です。別のディレクトリーから Express アプリケーションを実行する場合は、提供するディレクトリーの絶対パスを使用する方が安全です。
|
||||
|
||||
<pre>
|
||||
<code class="language-javascript" translate="no">
|
||||
app.use('/static', express.static(__dirname + '/public'));
|
||||
</code>
|
||||
</pre>
|
||||
```js
|
||||
const path = require('path')
|
||||
app.use('/static', express.static(path.join(__dirname, 'public')))
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user