Hello World の例
Embedded below is essentially the simplest Express app you can create. It is a single file app — not what you’d get if you use the Express generator, which creates the scaffolding for a full app with numerous JavaScript files, Jade templates, and sub-directories for various purposes.
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
This app starts a server and listens on port 3000 for connections. アプリケーションは、サーバーを始動して、ポート 3000 で接続を listen します。アプリケーションは、ルート URL (/
) または_ルート_ に対する要求に「Hello World!」と応答します。その他すべてのパスについては、「404 Not Found」と応答します。 For every other path, it will respond with a 404 Not Found.
Running Locally
最初に、myapp
という名前のディレクトリーを作成して、そのディレクトリーに移動し、npm init
を実行します。次に、インストール・ガイドに従い、依存関係として express
をインストールします。 Then, install express
as a dependency, as per the installation guide.
myapp
ディレクトリーで、app.js
というファイルを作成して、以下のコードを追加します。
req
(要求) と res
(応答) は、Node が提供するのとまったく同じオブジェクトであるため、Express が関与しない場合と同じように、req.pipe()
、req.on('data', callback)
などを呼び出すことができます。
次のコマンドを使用してアプリケーションを実行します。
$ node app.js
次に、ブラウザーに http://localhost:3000/ をロードして、出力を確認します。